473,382 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Put array into vector...

/*

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
4 3148

"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
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


"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: TF | last post by:
I have a fixed array of vectors like: vector<string> a_vStr; Then I grow each vector item and use it later. Everything works fine but I am curious about following potential problem. When we...
10
by: BCC | last post by:
Ive been googling and reading through my books but I haven't figured out a solution (much less an elegant one) to create a multidimensional array with a runtime determined number of dimensions. I...
49
by: vfunc | last post by:
If I have a large array 10,000+ elements then how do I reserve memory for this ? Currently I get a segmentation fault. Dynamic reservation is good, but allowing a chunk for the program is an...
20
by: Martin Jørgensen | last post by:
Hi, I'm reading a number of double values from a file. It's a 2D-array: 1 2 3 4 5 6 7 ------------- 1 3.2 2 0 2.1 3 9.3 4
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
9
by: JoeC | last post by:
I am crating a new version of my map game and my map will be a 2d array. I had problems trying to create a 2d array dynamically, in fact C++ won't let me do it. My question is how to create the...
3
by: Matthias Pospiech | last post by:
I have an 2D vector array, but a function that can calculate only with 1D arrays. Now I need to pass every row of the 2D array to that function. So how can I pass a part (row or column) of a 2D...
7
by: hlg | last post by:
I have a question, which must surely have occurred to many programmers since STL first appeared, and yet I have found no reference to it anywhere, suggesting the problem is insoluble. Nevertheless,...
6
by: remlostime | last post by:
now, i write some code code1: int a; int main(){} code2: vector<inta(10000000); int main(){} after using g++ compile, and run it, code1 is broken, but code2 runs
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.