0

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

Edited by jpetrey: n/a

2
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by Ancient Dragon
0

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.

0

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.

Edited by jpetrey: n/a

0

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.

Edited by Ancient Dragon: n/a

0

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.

Edited by jpetrey: n/a

0

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());
}

}

0

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.

0

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);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.