?
Solved

vector.push_back error....should be a quick fix...

Posted on 2003-03-01
11
Medium Priority
?
386 Views
Last Modified: 2012-05-04
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_back((v_filecontents[i].substr(0,sizeofline[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>,class std::allocator<char> >

Which points to the line:

v_lineindexed[size].push_back((v_filecontents[i].substr(0,sizeofline[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
0
Comment
Question by:nearlyanexpert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 8

Expert Comment

by:mnashadka
ID: 8048378
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]).
0
 

Author Comment

by:nearlyanexpert
ID: 8048445
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_back((v_filecontents[i].substr(0,sizeofline[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
0
 
LVL 11

Expert Comment

by:bcladd
ID: 8048467
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,sizeofline[i]);
}

Hope this helps, -bcl
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:nearlyanexpert
ID: 8049434
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?
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 8049622
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];
0
 

Author Comment

by:nearlyanexpert
ID: 8049738
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...
0
 

Author Comment

by:nearlyanexpert
ID: 8049740
ack...that didn't show correctly but you may get the genreal idea after reading the text.
0
 
LVL 8

Accepted Solution

by:
mnashadka earned 200 total points
ID: 8050578
It might be easier to use the resize function, since the vector has no copy constructor, like:
#include <vector>
#include <string>

using namespace std;

int main()
{
  const int size = 2;
  vector<vector<string> > v_linedindex(size); // Create a vector with 2 vectors
  v_linedindex[0].resize(5); // Make the first vector have 5 elements
  v_linedindex[1].resize(10); // Make the second have 10

  for(int i = 0; i < 5; ++i)
  {
    v_linedindex[0][i] = "whatever are you doing"; // Set the elements in the first
    v_linedindex[1][i] = "whatever are you doing";
  }

  return 0;
}
0
 

Expert Comment

by:icehands
ID: 8050949
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.
0
 

Author Comment

by:nearlyanexpert
ID: 8051814
Thank you!

thanks everybody else for your efforts...Although icehands, you just repeated what I said I was after :).

Nearlyanexpert question by question
0
 
LVL 11

Expert Comment

by:bcladd
ID: 8051836
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_back((v_filecontents[i].substr(0,sizeofline[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(vector());

  // 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(ndx); // chop off first
    } else { // at the end
      v_lineindexed[i].push_back(v_filecontents[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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question