Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C++ have anything like List<string> in C#?

Tags:

c++

c#

Does C++ has anything like List<> in C#? Something like List<string> for storing an array of strings.

like image 480
Ata Avatar asked Jun 12 '11 06:06

Ata


2 Answers

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.

like image 167
Sven Avatar answered Oct 19 '22 22:10

Sven


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;
like image 21
Alois Kraus Avatar answered Oct 19 '22 23:10

Alois Kraus