11

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

Sam
  • 7,252
  • 16
  • 46
  • 65
Ata
  • 12,126
  • 19
  • 63
  • 97
  • Note that "generics" are generally referred to as "templates" in C++. The standard template library provides a number of containers other than lists, too, if you ever need them. – Chris Frederick Jun 12 '11 at 07:00
  • though there is `std::list` you probably will want to use `std::vector`, or if it's static `std::array` – Gene Bushuyev Jun 12 '11 at 07:01
  • 3
    @Nawaz: The .NET `List` provides constant-time access to elements by index, much like `std::vector` and very much unlike `std::list`. – James McNellis Jun 12 '11 at 07:03

3 Answers3

21

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.

Sven
  • 21,903
  • 4
  • 56
  • 63
  • vector is not in vs , what header should I use ? – Ata Jun 12 '11 at 07:06
  • You need to `#include ` and `#include ` (for `std::string`). Additionally, you need to explicitly qualify the types as `std::vector` unless you do `using namespace std;` at the top of your C++ file. – Sven Jun 12 '11 at 07:09
  • 12
    @Ata: You need to take a step back, get [a good introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), and learn to program in C++ correctly, from the ground up. It is very difficult to learn C++ by trying to adapt code and idioms you are familiar with from other languages, and it is very easy to write horribly incorrect C++ code if you haven't learned to write C++ correctly. – James McNellis Jun 12 '11 at 07:11
  • 1
    I agree with James's sentiment. You seem to be lacking some fundamental knowledge about the differences between C++ and C#. – Sven Jun 12 '11 at 07:31
7

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;
Alois Kraus
  • 13,229
  • 1
  • 38
  • 64
0

C++ has std::vector template class that corresponds to C#'s List. It also has std::list template that corresponds to C# SingleLinkedList.

One must be prepared that in C++ vector and list call copy constructor of item. So, for each string you have a copy will be created.

So, if you are limited on memory or if you want to store the same strings in multiple collections, you'd better use std::vector<std::string*> or std::vector<char*> instead of std::vector<string>.

elder_george
  • 7,849
  • 24
  • 31
  • `std::list` is a doubly-linked-list, not a singly-linked-list. – Xeo Jun 12 '11 at 07:06
  • Sure, but BCL hasn't double-linked list and STL hasn't single-linked one. What I meant is existing classes are similar in terms of guaranteed insertion/deletion time. Thanks for the correction. – elder_george Jun 12 '11 at 07:09
  • 2
    It is ill-advised to have a container of pointers as such a thing is inherently not exception safe. In most circumstances, such a thing is not necessary anyway, and where such a thing might be necessary there are almost always far better solutions. – James McNellis Jun 12 '11 at 07:09