Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List<string> in C++

I'm cool with C# but am new to C++. I searched but found lots of different solutions which mostly do not work maybe because there are different versions of C++.

I'm using turbo C++ 4.5, I want something like C#'s List of strings

List<string> s = new List<string>();
s.Add("1");

I know a bit about C++ arrays, but I do not know the count of items at declaration time and that's why I want List-like solution so that I can declare once and add items later.

someone told me I should do it using pointers but I don't know how. Is it possible? or there any ways?

Please if you have an answer explain it cause I really like to learn, thanks.

like image 297
Mahdi Tahsildari Avatar asked Jan 19 '13 18:01

Mahdi Tahsildari


1 Answers

The equivalent to C# List<T> is std::vector<T>. The C++ code that corresponds to your C# code is this:

using namespace std;
....
vector<string> s;
s.push_back("1");

You should not take the advice to write such a class for yourself. Where they are appropriate, always use the standard containers.

like image 104
David Heffernan Avatar answered Oct 09 '22 01:10

David Heffernan