Does C++ has anything like List<>
in C#? Something like List<string>
for storing an array of strings.
The answer is actually
std::vector<std::string>
std::list
is a linked list, not an array like C#'s List<T>
class.
E.g.
#include <iostream> // iostream is for cout and endl; not necessary just to use vector or string
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> list;
list.push_back("foo");
list.push_back("bar");
for( vector<string>::const_iterator it = list.begin(); it != list.end(); ++it )
cout << *it << endl;
return 0;
}
The std::list
class is actually equivalent to C#'s LinkedList<T>
class.
A List in .NET is not a linked list. The data structure you are looking for is a resizeable array.
std::vector<std::string> list;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With