Put array into vector...

J

John Harrison

Oliver Gebele said:
/*

OK, after years i'm still more into C;
but i already do understand some C++.
And there are still many things about
the STL which i do not know...

I try to put 8-character-arrays in a vector
and when compiling on my Linux-box i get too
many errors to include here.

Can anybody tell me what my problem is?
And possibly how to fix it?

*/

Arrays are not copyable in C++. The reason you cannot put an array into a
vector is the same reason that you cannot do this

char a[8];
char b[8];
b = a; // error, arrays are not copyable

You have a couple of options (at least)

You could create a vector of vectors

vector< vector<char> > nodes;

or a vector of strings

vector< string > nodes;

but neither of these enforce the restriction you want (eight char arrays).
So probably a better option is to create an 8 character array class and use
that. Something like this

class EightCharArray
{
public:
EightCharArray();
EightCharArray(const char*);
char operator[](int i) const;
char& operator[](int i);
private:
char data[8];
};

vector< EightCharArray> nodes;
nodes.push_back("abcdefg");

I guess you can fill in the details of this class (and add some more) but
ask again if you need to.

john
 
B

Buster

Oliver said:
I try to put 8-character-arrays in a vector

You can't put an array into a standard container because arrays
do not model the CopyConstructible and Assignable requirements.

If you wrap the array in a class you can put objects of that
class into a container.

struct char_array_8 { char data [8]; };

Alternatively, if it's OK to have references rather than values
in the container, you might look into boost::shared_array.
 
O

Oliver Gebele

/*

OK, after years i'm still more into C;
but i already do understand some C++.
And there are still many things about
the STL which i do not know...

I try to put 8-character-arrays in a vector
and when compiling on my Linux-box i get too
many errors to include here.

Can anybody tell me what my problem is?
And possibly how to fix it?

*/

//vector.cc

using namespace std;

#include <cstdio>
#include <vector>

int main()
{
char string8[8]="abcdefg";
vector<char[8]> nodes;

nodes.push_back(string8);

return 0;
}
 
T

Thorsten Ottosen

|
[snip]
| You have a couple of options (at least)
|
| You could create a vector of vectors
|
| vector< vector<char> > nodes;

a sound advice. Using std::vector< boost::array<char,7> > is less flexible but enforces
a size on the arrays.

br

Thorsten

PS: you can find the array class at www.boost.org
 
O

Oliver Gebele

OK, Thanks on this one.
As is seen I'm not aware of the
restrictions using the STL.

Thankx, Oliver

John said:
Oliver Gebele said:
/*

OK, after years i'm still more into C;
but i already do understand some C++.
And there are still many things about
the STL which i do not know...

I try to put 8-character-arrays in a vector
and when compiling on my Linux-box i get too
many errors to include here.

Can anybody tell me what my problem is?
And possibly how to fix it?

*/

Arrays are not copyable in C++. The reason you cannot put an array into a
vector is the same reason that you cannot do this

char a[8];
char b[8];
b = a; // error, arrays are not copyable

You have a couple of options (at least)

You could create a vector of vectors

vector< vector<char> > nodes;

or a vector of strings

vector< string > nodes;

but neither of these enforce the restriction you want (eight char arrays).
So probably a better option is to create an 8 character array class and
use that. Something like this

class EightCharArray
{
public:
EightCharArray();
EightCharArray(const char*);
char operator[](int i) const;
char& operator[](int i);
private:
char data[8];
};

vector< EightCharArray> nodes;
nodes.push_back("abcdefg");

I guess you can fill in the details of this class (and add some more) but
ask again if you need to.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top