How to Store Arrays in an Stl List

How do I store arrays in an STL list?

You can't store arrays in STL containers. You'd use a vector of vectors or somesuch for the general case. For your specific case, I'd use a vector of std::pair, like so: std::vector<std::pair<int, int> >. std::pair is a class that has two members, first and second, of whatever type you templatize it to be.

Edit: I originally had it as std::vector<std::pair<int> >, but I wasn't sure if it was overloaded to accept only 1 parameter in the case that both types are the same... a little digging turned up no evidence of this, so I modified it to explicitly state that both first and second are ints.

array of std::list c

Your declaration is incorrect, and not supported in C++. You're trying to declare a variable-sized array at run-time, which IS a C99 feature, but is not in C++. See here: Array size at run time without dynamic allocation is allowed?

You have this:

std::list<int> mylist[size]

What you need is this:

std::list<int> mylist[] = new std::list<int>[size];

But this is still a bad idea, as you need to de-allocate it later with delete []. As others have said, you can do it a couple of different ways, all of which are better for c++:

std::list< std::list< int > > myListOfLists;      // Linked list of linked lists
std::vector< std::list< int > > myVectorOfLists; // Better, a vector is more like an array
std::array<std::list<int>, 10> this_would_do; // From above, only works if you know the array size at compile-time

I hope this helps and is clear for you.

Correct way to work with vector of arrays

You cannot store arrays in a vector or any other container. The type of the elements to be stored in a container (called the container's value type) must be both copy constructible and assignable. Arrays are neither.

You can, however, use an array class template, like the one provided by Boost, TR1, and C++0x:

std::vector<std::array<double, 4> >

(You'll want to replace std::array with std::tr1::array to use the template included in C++ TR1, or boost::array to use the template from the Boost libraries. Alternatively, you can write your own; it's quite straightforward.)

How can I make a std::list of arrays?

typedef std::vector<char> char_array;
typedef std::list<char_array> char_array_list;

Or more appropriately:

typedef std::list<std::string> string_list;

STL stack with elements of array

As you want to store only two values of different datatypes, you can use std::pair.

  1. Create a stack<pair<int,string> >.
  2. To push the pair<int,string> into the stack, use push() function and the make_pair function to make a pair for the push() function.

Code:

stack<pair<int,string> > s;
s.push(make_pair(1,string("item")));
s.push(make_pair(3,string("item")));

EDIT: (Thanks to @imlyc)

If you enable the -std=c++11 flag when compiling with g++, you can replace

s.push(make_pair(1,string("item")));

with

s.push({1,"item"});

how to store count of values that are repeated in an array into map in c++?

The reason for the error is that list is an array, which does not have a begin method (or any other method).

This could be fixed by changing the function to take a std::vector instead of an array.

If you want to keep it as an array, the for loop should be changed to this, assuming n is the length of the array:

for(auto val = list; val != list + n; val++)

In C and C++, an array is somewhat equivalent to a pointer to the first element of the array; thus list gives the start pointer, and list + n gives a pointer to after the end of the array.



Related Topics



Leave a reply



Submit