nearlyanexpert
asked on
vector.push_back error....should be a quick fix...
Hi,
I've declared a vector as follows:
vector <string> v_lineindexed(size);
Then later on...
for(i = 0; i < v_filecontents.size();i++)
{
size = (v_filecontents[i].find(", "));
v_lineindexed[size].push_b ack((v_fil econtents[ i].substr( 0,sizeofli ne[i])));
}
What I'm wanting this to do is push_back the substring that is found in v_filecontents to v_lineindexed[size].
However I'm getting a:
error C2039: 'push_back' : is not a member of 'basic_string<char,struct std::char_traits<char>,cla ss std::allocator<char> >
Which points to the line:
v_lineindexed[size].push_b ack((v_fil econtents[ i].substr( 0,sizeofli ne[i])));
Here are the way things are declared:
vector <string> v_filecontents;
int size;
vector <string> v_lineindexed(size);
sizeofline is simply an integer, which is no way related to the problem.
Any body help please?
Thank you,
Nearlyanexpert
I've declared a vector as follows:
vector <string> v_lineindexed(size);
Then later on...
for(i = 0; i < v_filecontents.size();i++)
{
size = (v_filecontents[i].find(",
v_lineindexed[size].push_b
}
What I'm wanting this to do is push_back the substring that is found in v_filecontents to v_lineindexed[size].
However I'm getting a:
error C2039: 'push_back' : is not a member of 'basic_string<char,struct std::char_traits<char>,cla
Which points to the line:
v_lineindexed[size].push_b
Here are the way things are declared:
vector <string> v_filecontents;
int size;
vector <string> v_lineindexed(size);
sizeofline is simply an integer, which is no way related to the problem.
Any body help please?
Thank you,
Nearlyanexpert
v_linedindex is the vector, but v_lineindexed[size] is an element of the vector, which is a string. Try v_lineindexed.push_back (without the [size]).
ASKER
That's what I'm wanting though,
the:
vector <string> v_lineindexed(size);
tells it to create an array of vectors called v_lineindexed. In the
v_lineindexed[size].push_b ack((v_fil econtents[ i].substr( 0,sizeofli ne[i])));
This tells the element of v_lineindexed to add something to it.
So for example is size was 3 then:
v_lineindexed[0];
v_lineindexed[1];
v_lineindexed[2];
Can't I just then add a push_back to each one but instead of actually putting a number in the [] I just use the size.
I've got a feeling something is wrong there, but that's the basic idea which I'm wanting...have a variable number of v_lineindexed created then access each one like I tried above...
Hopefully that may help,
NAE
the:
vector <string> v_lineindexed(size);
tells it to create an array of vectors called v_lineindexed. In the
v_lineindexed[size].push_b
This tells the element of v_lineindexed to add something to it.
So for example is size was 3 then:
v_lineindexed[0];
v_lineindexed[1];
v_lineindexed[2];
Can't I just then add a push_back to each one but instead of actually putting a number in the [] I just use the size.
I've got a feeling something is wrong there, but that's the basic idea which I'm wanting...have a variable number of v_lineindexed created then access each one like I tried above...
Hopefully that may help,
NAE
push_back is not part of std::string.
If you want to append a string to another string you can just use the overloaded + operator:
for(i = 0; i < v_filecontents.size();i++)
{
size = (v_filecontents[i].find(", "));
v_lineindexed[size] += v_filecontents[i].substr(0 ,sizeoflin e[i]);
}
Hope this helps, -bcl
If you want to append a string to another string you can just use the overloaded + operator:
for(i = 0; i < v_filecontents.size();i++)
{
size = (v_filecontents[i].find(",
v_lineindexed[size] += v_filecontents[i].substr(0
}
Hope this helps, -bcl
ASKER
I'm actually wanting to add to a certain array though and not just the entire thing...Hmm..prehaps a better explaination may help a bit :-)
I'm wanting something like the following:
vector <string> vec(largestsize)
and so creates:
Vec1[0]
Vec1[1]
Vec1[2]
Vec1[3]
Vec1[4]
...
...
this goes on for (largestsize)
I then want to be able to do something like the following:
vec1[0].push_back(...);
vec1[0].push_back(...);
but because I've got a brace [] in there it complains that push_back isn't of type string...
Anybody?
I'm wanting something like the following:
vector <string> vec(largestsize)
and so creates:
Vec1[0]
Vec1[1]
Vec1[2]
Vec1[3]
Vec1[4]
...
...
this goes on for (largestsize)
I then want to be able to do something like the following:
vec1[0].push_back(...);
vec1[0].push_back(...);
but because I've got a brace [] in there it complains that push_back isn't of type string...
Anybody?
I think your problem is that you're putting the size in parentheses, which actually creates a vector with size elements. What you really want is to put the size in brackets, and this will create an array.
#define size 10
...
vector <string> v_lineindexed[size];
#define size 10
...
vector <string> v_lineindexed[size];
ASKER
no it doesn't...using v_lineindexed[size] refers to the vector item at position size...
I've been looking at 2D vectors for this problem, i.e. vectors of vectors, although:
typedef vector <string> v_data;
typedef vector <v_data> v_lineindexedt;
v_lineindexedt v_lidt;
However whenever I try to use v_lidt I get a runtime memory error about it not being able to access memory at an address...I don't know if vectors of vectors is what I'm wanting but, again...
~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~
~PLEASE LOOK HERE FOR EXPLAINATION~
~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~
Here's a different idea to what I'm wanting:
Vect1 [ 0 , 1 , 2 , 3 , 4 , 5 , ....]
[0, [0,
1, 1,
2, 2,
3] 3,
4,
5,
6]
There can be any number of vectors in vect1 and inside each of those elements there can be any number of elements. In this case Vect1[0] has 4 elements inside it, although that may easily grow up to anything. Vect1[3] has 7 elements inside it. However all the elements inside the elements of vect[] are all of string type. I know how many elements vect1 will have maximum at the time they are created. Can anybody help me out with this one then? I.e. how to create something like this and how to access the members?
Thanks...
I've been looking at 2D vectors for this problem, i.e. vectors of vectors, although:
typedef vector <string> v_data;
typedef vector <v_data> v_lineindexedt;
v_lineindexedt v_lidt;
However whenever I try to use v_lidt I get a runtime memory error about it not being able to access memory at an address...I don't know if vectors of vectors is what I'm wanting but, again...
~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~
~PLEASE LOOK HERE FOR EXPLAINATION~
~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~
Here's a different idea to what I'm wanting:
Vect1 [ 0 , 1 , 2 , 3 , 4 , 5 , ....]
[0, [0,
1, 1,
2, 2,
3] 3,
4,
5,
6]
There can be any number of vectors in vect1 and inside each of those elements there can be any number of elements. In this case Vect1[0] has 4 elements inside it, although that may easily grow up to anything. Vect1[3] has 7 elements inside it. However all the elements inside the elements of vect[] are all of string type. I know how many elements vect1 will have maximum at the time they are created. Can anybody help me out with this one then? I.e. how to create something like this and how to access the members?
Thanks...
ASKER
ack...that didn't show correctly but you may get the genreal idea after reading the text.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sounds to me like you want a `vector' of `vectors' containing `strings'.
All you have to do is change your declaration of vector to
vector < vector<string> > v_lineindexed(size);
Each index of v_lineindexed will then store an array of strings.
All you have to do is change your declaration of vector to
vector < vector<string> > v_lineindexed(size);
Each index of v_lineindexed will then store an array of strings.
ASKER
Thank you!
thanks everybody else for your efforts...Although icehands, you just repeated what I said I was after :).
Nearlyanexpert question by question
thanks everybody else for your efforts...Although icehands, you just repeated what I said I was after :).
Nearlyanexpert question by question
It is not clear to me that you need to use resize/sized constructor for vector at all:
Your code from above:
for(i = 0; i < v_filecontents.size();i++)
{
size = (v_filecontents[i].find(", "));
v_lineindexed[size].push_b ack((v_fil econtents[ i].substr( 0,sizeofli ne[i])));
}
v_filecontents contains the lines (presumably read from an input file) that are comma-delimited strings. You want the elements broken out and put into the vector v_lineindexed vectors.
So, let's try a vector of vectors.
vector< vector<string> >v_lineindexed;
// load v_filecontents
for (int i=0; i<v_filecontents.size(); ++i) {
// now we need a new entry for the current line in
// v_lineindexed:
v_lineindexed.pushback(vec tor());
// Now destructively parse the line in v_filecontents
// assumption: comma delimited line to be broken at the
// commas (ignoring quoted strings (containing commas)
// and whitespace issues)
while (v_filecontents[i].size() > 0) {
string::size ndx = v_filecontents[i].find("," );
if (ndx != string::npos) { // if comma was found
v_lineindexed[i].push_back (
v_filecontents[i].substr(0 , ndx)); // copy first
v_filecontents[i] =
v_filecontents[i].substr(n dx); // chop off first
} else { // at the end
v_lineindexed[i].push_back (v_filecon tents[i]);
v_filecontents[i] = "";
}
} // for all lines
Now v_lineindexed has all of the entries from the file. To print it all out again (with ... between the elements so that we can see the changes):
for (unsigned int i = 0; i < v_lineindexed.size(); ++i) {
string delimiter = "";
for (unsigned int j = 0; j < v_lineindexed[i].size(); ++j) {
cout << delimiter << v_lineindexed[i][j];
delimiter = "...";
} // for j
cout << endl;
} // for i
Hope this helps, -bcl
Your code from above:
for(i = 0; i < v_filecontents.size();i++)
{
size = (v_filecontents[i].find(",
v_lineindexed[size].push_b
}
v_filecontents contains the lines (presumably read from an input file) that are comma-delimited strings. You want the elements broken out and put into the vector v_lineindexed vectors.
So, let's try a vector of vectors.
vector< vector<string> >v_lineindexed;
// load v_filecontents
for (int i=0; i<v_filecontents.size(); ++i) {
// now we need a new entry for the current line in
// v_lineindexed:
v_lineindexed.pushback(vec
// Now destructively parse the line in v_filecontents
// assumption: comma delimited line to be broken at the
// commas (ignoring quoted strings (containing commas)
// and whitespace issues)
while (v_filecontents[i].size() > 0) {
string::size ndx = v_filecontents[i].find(","
if (ndx != string::npos) { // if comma was found
v_lineindexed[i].push_back
v_filecontents[i].substr(0
v_filecontents[i] =
v_filecontents[i].substr(n
} else { // at the end
v_lineindexed[i].push_back
v_filecontents[i] = "";
}
} // for all lines
Now v_lineindexed has all of the entries from the file. To print it all out again (with ... between the elements so that we can see the changes):
for (unsigned int i = 0; i < v_lineindexed.size(); ++i) {
string delimiter = "";
for (unsigned int j = 0; j < v_lineindexed[i].size(); ++j) {
cout << delimiter << v_lineindexed[i][j];
delimiter = "...";
} // for j
cout << endl;
} // for i
Hope this helps, -bcl