ok i having a little trouble adding a string to the new vector that i created i was wondering if i need this line

vector<string>* row = new vector<string>;

and if i do i need to keep calling or is there a much better what to create a new row
heres a sample of the text file that we have to read in
Namibe # - # Mocamedes # NAM # Angola # Africa # F # 12076 # -1 # -1 # #
#
every string is sepearted by a # and a new row is seperated by a extra # and the end of the file has ###

StringTable::StringTable(ifstream & infile)
{
   vector<string>* row = new vector<string>;
   int i = 0,pound = 0;
   string Tname;
   bool sign;
   while(pound != 3)
   {
        sign = readMultiWord(Tname,infile);
        if(sign == false)
          {
            pound++;
            if(pound != 2)
            {
              row->push_back(Tname);
              i++;
            }
          }
        else if(pound == 2)
          {
            table[i].push_back(Tname);
            //table[i].push_back(*row);
            pound = 0;
          }
   }
}
bool readMultiWord(string & s, ifstream & infile, const string & sep)
{  
    string  w;

    infile >> s;
    if (s == sep)
        return false;
    infile >> w;
    while (w != sep)
    {
        s += " " + w;
        infile >> w;
    }
    return true;
}

Recommended Answers

All 6 Replies

I think what you are looking for is a 2d vector. If that is what you are trying to do than to declare a 2d vector use

std::vector<std::vector<string> > rows;

when you set up the std::vector<std::vector<string> > rows; how should the string table look?

it should just be all the Namibe - Mocamedes NAM Angola Africa F 12076 -1 -1 just without all the pounds so the row should be 1 and columns should be 10

yes but their is several lines in the text file each one needs its own row

std::vector<std::vector<string> > rows; is not the main vector just need a way to store a whole line in a temporary vector than put it in this one vector< vector<string> > table;

If this is only a temp to store the line from the file, use a std::string object. This way you should be able to avoid an extra vector* and do something like this:

std::string inputLine;
getline(inFile, inputLine);

Then analyze/tokenize inputLine and push the substrings into your final vector.

*A std::string is essentially a std::vector<char>, it has essintially the same interface as a vector (with some minor differences). This gives you the ability to manipulate it in a fashion similar to a vector.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.