我正在尝试根据给定值插入数据 - strBeg。我希望矢量按照这个值进行数字排序。有两种不同的载体。我的驱动程序如下所示,我希望输出为:
linei[0][0] = 1 linei[0][1] = 8 refi[0][0] = 81 refi[0][1] = 88
linei[1][0] = 21 linei[1][1] = 31 refi[1][0] = 10 refi[1][1] = 20
linei[0][0] = 33 linei[0][1] = 44 refi[0][0] = 0 refi[0][1] = 11
linei[1][0] = 45 linei[1][1] = 47 refi[1][0] = 6 refi[1][1] = 8
正如您所看到的,refBeg / refEnd的值不会影响顺序,但必须保持其strBeg / strEnd对。我在下面发布了我的代码......它不起作用。我当前的方法需要一个非常大的排序功能来解决许多不同的情况,我想尽可能避免这种情况。我想知道是否有更有效的方法来实现它?我想把两个向量组合成一个有4列的但不会因为这会让它更难以理解(但如果它是最好/最简单的选择,我不会完全反对)。 linei中也没有重叠,因为不会有两组具有相同strBeg和strBeg / strEnd的数据不会落入另一个字符串的起点和终点之间。我读了一些其他类似的问题,但无法弄清楚如何使它适应我的情况。任何帮助将不胜感激!!! 注意:如果我今晚不回复,我会在早上。再次感谢!
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void insertData(const int refBeg, const int refEnd, const int strBeg, const int strEnd, vector<vector<int> >& refi, vector<vector<int> >& linei);
int main(int argc, const char * argv[])
{
vector<vector<int> > refi;
vector<vector<int> > linei;
insertData(0, 11, 33, 44, refi, linei);
insertData(10, 20, 21, 31, refi, linei);
insertData(6, 8, 45, 47, refi, linei);
insertData(80, 88, 1, 8, refi, linei);
for (int i=0; i<linei.size(); i++) {
cout << "linei[" << i << "][0] = " << linei[i][0] << " ";
cout << "linei[" << i << "][1] = " << linei[i][1] << " ";
cout << "refi[" << i << "][0] = " << refi[i][0] << " ";
cout << "refi[" << i << "][1] = " << refi[i][1] << endl;
}
return 0;
}
void insertData(const int refBeg, const int refEnd, const int strBeg, const int strEnd, vector<vector<int> >& refi, vector<vector<int> >& linei) {
linei.push_back(vector<int>() );//creates a new row in linei
refi.push_back(vector<int>() );//creates a new row in refi
int size=(int)linei.size();
if ((size-1) == 0) {
linei[0].push_back(strBeg);
linei[0].push_back(strEnd);
refi[0].push_back(refBeg);
refi[0].push_back(refEnd);
} else {
for (int i=0; i<size; i++) {
if (strBeg > linei[i][0]) {
linei[i+1].push_back(strBeg);
linei[i+1].push_back(strEnd);
refi[i+1].push_back(refBeg);
refi[i+1].push_back(refEnd);
break;
}
}
}
}
工作代码:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct line_ref
{
vector<int> linei;
vector<int> refi;
bool operator<(const line_ref &rhs) const { return linei[0] < rhs.linei[0]; }
};
void insertData(const int refBeg, const int refEnd, const int strBeg, const int strEnd, vector<line_ref>& line_ref_i);
int main(int argc, const char * argv[])
{
vector<line_ref> line_ref_i;
insertData(0, 11, 33, 44, line_ref_i);
insertData(10, 20, 21, 31, line_ref_i);
insertData(6, 8, 45, 47, line_ref_i);
insertData(80, 88, 1, 8, line_ref_i);
cout << "UNSORTED\n";
for (int i=0; i<line_ref_i.size(); i++) {
cout << "LINEI[0] = " << line_ref_i[i].linei[0] << " ";
cout << "LINEI[1] = " << line_ref_i[i].linei[1] << " ";
cout << "REFI[0] = " << line_ref_i[i].refi[0] << " ";
cout << "REFI[1] = " << line_ref_i[i].refi[1] << endl;
}
sort(line_ref_i.begin(), line_ref_i.end() );//, /*??*/);
cout << "SORTED\n";
for (int i=0; i<line_ref_i.size(); i++) {
cout << "LINEI[0] = " << line_ref_i[i].linei[0] << " ";
cout << "LINEI[1] = " << line_ref_i[i].linei[1] << " ";
cout << "REFI[0] = " << line_ref_i[i].refi[0] << " ";
cout << "REFI[1] = " << line_ref_i[i].refi[1] << endl;
}
return 0;
}
void insertData(const int refBeg, const int refEnd, const int strBeg, const int strEnd, vector<line_ref>& line_ref_i) {
line_ref_i.push_back(line_ref() );
int size = (int)line_ref_i.size() - 1;
line_ref_i[size].linei.push_back(strBeg);
line_ref_i[size].linei.push_back(strEnd);
line_ref_i[size].refi.push_back(refBeg);
line_ref_i[size].refi.push_back(refEnd);
}
答案 0 :(得分:0)
为什么不将相关元素放入struct
?
struct line_ref
{
vector<int> linei;
vector<int> refi;
};
vector <line_ref> line_ref_i;
或者,如果您不介意字段的struct
和std::pair<int,int>
等通用名称,则可以使用first
,而不是声明second
。