I need to sort a table by column. My tables are represents by a single vector.
example :
col_name A B C
vector : 1 2 3 6 5 4 7 8 9
that give me the table :
A B C
1 6 7
2 5 8
3 4 9
After a sort on column B , I need to obtain :
A B C
3 4 9
2 5 8
1 6 7
my code :
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
int main()
{
std::vector<std::string> vec = {"1","8","1","2","3","2","3",
"5","5","2","5","6","5","6",
"9","3","3","4","8","3","9"};
int size = 7;
int col_idx = 1;
for(int i = 0; i<3;++i)
{
if(i==col_idx)
continue;
std::sort(vec.begin() + i*size, vec.begin() + (i+1)*size,
[col_idx, size, i](std::string& s1,std::string& s2)
{
std::cout << s1 << " "
<< s2 << " "
<< *(&s1 +(col_idx - i)*size) << " "
<< *(&s2 +(col_idx - i)*size) << " "
<< (*(&s1 +(col_idx - i)*size) < *(&s2 +(col_idx - i)*size)) << std::endl;
return *(&s1 +(col_idx - i)*size) < *(&s2 +(col_idx - i)*size);
});
}
std::sort(vec.begin() + col_idx*size, vec.begin() + (col_idx+1)*size);
}
I have a segmentation fault error : only the first line appear from the cout.
Aucun commentaire:
Enregistrer un commentaire