i am reading in data from an input file and sorting it as it comes in. (ive done this before just without sorting so i know everything else works fine.)

StringTable::StringTable(ifstream & infile)
{
    // TODO: complete

    string          s;
    vector<string>  row;
    
    vector<      vector<string> >::iterator srow = table.begin();
    vector<      vector<string> >::iterator erow = table.end();
    vector<vector<string> >::iterator i;
    vector<string>::iterator i2;

    i=table.begin();
    //i2=(*i).begin();


    while (readMultiWord(s, infile))  // not end of file
    {
        row.clear();
        do
        {
         
            row.push_back(s);
        
        }
        while (readMultiWord(s, infile));

     if (table.size()==0)
     {
        table.push_back(row);
     }
     else
     {
            vector<string>::iterator news=row.begin();
         for (int i=0; i<table.size(); i++)
         {
          erow = table.end();
          cout << "hi..." << endl;
          vector<string>::iterator scol = srow->begin();
                cout <<"hi..." << endl;
          cout << *scol << " " << *news << endl;
          if (*scol<*news)
          {
            table.push_back(vector<string>());
              table.insert(srow, row);
            cout << *scol << " " << *news << endl;
          } 
          srow++;
            }
         }           
    }
}

when i create the iterator scol it is throwing me a seg fault, and i cant seem to figure out why. if anyone could help that would be fantastic.

update: got past the seg fault but now it gets through about three of the names and throughs an error. terminate called after throwing an instance of 'std::bad_alloc'
what(): St9bad_alloc
Abort

It would be a lot simpler and faster to wait until all the data has been read and then sort it. Just use std::sort() to sort all the data in the vector at one time.

would that really work for a 2dvector?

would it just be table.sort();?

i tried that and it threw me an error saying sort is not a member function.

It's not table.sort() but std::sort(table.begin(),table.end(),table.begin()); Include <algorithm>

Since table is a 2d vector (of what I don't know) you will most likely have to also code a comparison function that compares two table entries.

It's not table.sort() but std::sort(table.begin(),table.end(),table.begin());

Since table is a 2d vector (of what I don't know) you will most likely have to also code a comparison function that compares two table entries.

ill try that out and sorry its a 2d vector of strings.
i am supposed to sort the vector from a to z.

just read over the instructions. we are supposed to d it using at iterators. i think hes making it overly complicated just for sake of making things complicated.

So you have vector<vector<string>> table; ? Do you want to sort the rows of the table, or sort the strings in each of the rows?

If the first row contains "b" and "a" the result should be "a" and "b"?
That will simplify the sort

vector<vector<string>>::iterator tbit;
vector<string>::iterator it;

for(tbit = table.begin(); tbit != table.end(); tbit++)
{
    vector<string>& row = *tbit;
    std::sort(row.begin(),row.end(), row.begin());
}

}

So you have vector<vector<string>> table; ? Do you want to sort the rows of the table, or sort the strings in each of the rows?

If the first row contains "b" and "a" the result should be "a" and "b"?
That will simplify the sort

vector<vector<string>>::iterator tbit;
vector<string>::iterator it;

for(tbit = table.begin(); tbit != table.end(); tbit++)
{
    vector<string>& row = *tbit;
    std::sort(row.begin(),row.end(), row.begin());
}

}

sorry for being unclear. we are to sort the rows. the input file starts each row with the name of a country such as "iraq", "spain", etc.. we are to sort the rows of the 2d vector so they are in order.

Then do something like this

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using std::cout;
using std::string;
using std::vector;
using std::sort;

int compare(vector<string>& s1, vector<string>& s2)
{
    return s1[0] < s2[0];
}

int main()
{
    vector<vector<string>> table;
    sort(table.begin(), table.end(), compare);
}
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.