By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Put array into vector...

P: n/a
/*

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;
}

Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Oliver Gebele" <ol***********@tiscali.de> wrote in message
news:c7***********@ulysses.news.tiscali.de...
/*

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
Jul 22 '05 #2

P: n/a
Oliver Gebele wrote:
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.

--
Regards,
Buster.
Jul 22 '05 #3

P: n/a


"John Harrison" <jo*************@hotmail.com> wrote in message news:2g***********@uni-berlin.de...
|
[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


Jul 22 '05 #4

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

Thankx, Oliver

John Harrison wrote:

"Oliver Gebele" <ol***********@tiscali.de> wrote in message
news:c7***********@ulysses.news.tiscali.de...
/*

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


Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.