0

I am stuck trying to bubble sort a vector<vector<string>> vvs If running a for loop like

for ( auto x : vvs ) which contains a line like

if ( x.at(pos) == (*need next x position*).at(pos) {
    //perform sort and swap if needed
}

Is it possible to get the next position of the range based loop?

TheUnknown
  • 53
  • 2
  • 10

3 Answers3

1
for (auto i : o)
    std::cout << i << std::endl;

range based for loop are used only for sequencing each element in an array or vector for customization loops used traditional for loop

for (unsigned i = 2; i != 10; ++i)
      std::cout << arr[i] << std::endl; 
Kryssel Tillada
  • 180
  • 3
  • 13
1

Insted of using a vector, just use a std::list, Its much simpler and faster than manually doing it,

std::list<std::string> vvs;

And then to order the list its as easy as:

vvs.sort(compare_nocase);

To sort the list alphabetically, and not case specific;

Kieren Pearson
  • 416
  • 3
  • 15
1

Alternatively, define an iterator based Bubble Sort like

template <class Iterator>
inline void BubbleSort(Iterator begin, Iterator end) {
    for (Iterator i = begin; i != end; ++i)
        for (Iterator j = begin; j < i; ++j)
            if (*i < *j)
                std::iter_swap(i, j);
}

To apply on your vector

for (auto & v : vvs) {
    BubbleSort(v.begin(), v.end());
}
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50