-3

Would

string Example[ ];

be an array with an undefined amount of values? If so could I import values from a .txt file into it even if I didn't know how many items there are?

ChessRobot
  • 11
  • 1
  • 4
  • 1
    It doesn't even compile... – Rakete1111 Aug 03 '18 at 15:11
  • 2
    No. This is why we have `std::vector`. – NathanOliver Aug 03 '18 at 15:11
  • no you cant. Tbh your question is a bit funny, because `string` already can grow in size... If you really want many strings use a `std::vector` – 463035818_is_not_an_ai Aug 03 '18 at 15:11
  • 2
    You can us a `std::vector` – πάντα ῥεῖ Aug 03 '18 at 15:11
  • 1
    Possible duplicate of [How do I use arrays in C++?](https://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) – Max Langhof Aug 03 '18 at 15:12
  • dude how was i supposed to know that. it has to be one of the most broad questions. – ChessRobot Aug 03 '18 at 15:23
  • @ChessRobot "_how was i supposed to know that. it has to be one of the most broad questions._" I disagree. One could've searched for "C++ array declaration", and arrived at this page: https://en.cppreference.com/w/cpp/language/array , or one could've searched for "C++ array declaration without size", and arrived at: https://stackoverflow.com/questions/17259098/how-to-declare-an-array-without-specific-size . Or, alternatively, one could learn from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), instead of coding randomly. – Algirdas Preidžius Aug 03 '18 at 15:43

1 Answers1

7

C++ does not support variable length arrays. Instead, you could use std::vector:

std::vector<std::string> Example;
Example.push_back("some string");
std::cout << Example.size() << '\n'; // prints 1

Vectors can contain as many elements as you like, and are automatically resized when you add new ones.

Bruce Collie
  • 435
  • 3
  • 8