Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 408
  • Last Modified:

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_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
nearlyanexpert
Asked:
nearlyanexpert
  • 5
  • 3
  • 2
  • +1
1 Solution
 
mnashadkaCommented:
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
 
nearlyanexpertAuthor Commented:
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
 
bcladdCommented:
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
Industry Leaders: 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!

 
nearlyanexpertAuthor Commented:
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
 
mnashadkaCommented:
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
 
nearlyanexpertAuthor Commented:
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
 
nearlyanexpertAuthor Commented:
ack...that didn't show correctly but you may get the genreal idea after reading the text.
0
 
mnashadkaCommented:
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
 
icehandsCommented:
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
 
nearlyanexpertAuthor Commented:
Thank you!

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

Nearlyanexpert question by question
0
 
bcladdCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now