我正在使用一个名为ITV的地图模板,其中定义的结构作为键值,一组整数作为地图值。出于任何原因,insert命令对某些键值不起作用。
我已将代码减少到尽可能小,但它仍然很大。它可以编译和执行,只要将一对添加到某个域的映射值,就会返回一行,指示当时关键值和关联映射值的大小。
不幸的是,在函数intervals()
的某个时刻(在初始弧的情况下),insert命令什么都不做:
ITV[Domain(PairStr,q)].insert(make_pair(t,t)); // ERROR! DOES NOT WORK
我想这可能与地图类的某些内部排序有关,因为错误只在PairStr
为("INI", "A")
的情况下发生,PairStr.first > PairStr.second
是唯一的情况,但我真的对此没有任何想法。
#include <iostream>
#include <vector>
#include <map>
#include <iterator>
#include <string>
#include <set>
using namespace std;
typedef set<pair<string,string> > SPS;
typedef set<pair<string,string> >::iterator SPSIt;
struct Domain {
string i;
string j;
int q;
Domain() : i("NaNode"), j("NaNode"), q(-1) {}
Domain(string oi, string oj, int oq) : i(oi), j(oj), q(oq) {}
Domain(pair<string,string> ops, int oq) : i(ops.first), j(ops.second), q(oq) {}
bool operator< (Domain const &domain) const {
return i < domain.i || j < domain.j || q < domain.q;
}
};
// GRAPH STRUCTURE
SPS ARCS, INIT_ARCS, DEST_ARCS, TOTAL_ARCS;
map<Domain ,set<pair<int,int> > > ITV;
// TIME and SO ON
int T_MAX, Q_MAX; // number of periods and stages
vector<int> STG_first; // first and last period of stage q
vector<int> STG_last;
void intervals(){
pair<string,string> PairStr; // auxiliar variable
// loop for generating intervals
for(SPSIt it = TOTAL_ARCS.begin(); it != TOTAL_ARCS.end(); ++it)
for(int q = 0; q < Q_MAX; ++q)
for(int t = STG_first[q]; t<= STG_last[q]; ++t){
PairStr = *it;
// Case regular arc
if (ARCS.count(PairStr)){
for(int v = t; v <= STG_last[q]; ++v){
ITV[Domain(PairStr,q)].insert(make_pair(t,v));
cout << "Size of " << PairStr.first << "-" << PairStr.second << " at " << q << ": " << ITV[Domain(PairStr,q)].size() << endl;
}
// Case initial arc
} else if (INIT_ARCS.count(PairStr)){
ITV[Domain(PairStr,q)].insert(make_pair(t,t)); // ERROR! DOES NOT WORK
cout << "Size of " << PairStr.first << "-" << PairStr.second << " at " << q << ": " << ITV[Domain(PairStr,q)].size() << endl;
// Case destination arc
} else if (DEST_ARCS.count(PairStr) && q > 0){
ITV[Domain(PairStr,q)].insert(make_pair(t,t));
cout << "Size of " << PairStr.first << "-" << PairStr.second << " at " << q << ": " << ITV[Domain(PairStr,q)].size() << endl;
}
}// end loop
}// end intervals()
int main (){
// read arcs
ARCS.insert(make_pair("A", "B"));
TOTAL_ARCS.insert(make_pair("A", "B"));
INIT_ARCS.insert( make_pair("INI", "A"));
TOTAL_ARCS.insert(make_pair("INI", "A"));
DEST_ARCS.insert( make_pair("B", "DES"));
TOTAL_ARCS.insert(make_pair("B", "DES"));
// read number of periods and stages
T_MAX = 3;
Q_MAX = 2;
STG_first = vector<int> (Q_MAX);
STG_last = vector<int> (Q_MAX);
STG_first[0] = STG_last[0] = 0;
STG_first[1] = 1;
STG_last[1] = 2;
intervals();
return 0;
} // END main
所需的输出如下:
Size of A-B at 0: 1
Size of A-B at 1: 1
Size of A-B at 1: 2
Size of A-B at 1: 3
Size of B-DES at 1: 1
Size of B-DES at 1: 2
Size of INI-A at 0: 1
Size of INI-A at 1: 1
Size of INI-A at 1: 2
但目前的输出是这个:
Size of A-B at 0: 1
Size of A-B at 1: 1
Size of A-B at 1: 2
Size of A-B at 1: 3
Size of B-DES at 1: 1
Size of B-DES at 1: 2
Size of INI-A at 0: 0
Size of INI-A at 1: 0
Size of INI-A at 1: 0
对于这篇长篇文章感到抱歉,我将不胜感激任何暗示。
答案 0 :(得分:2)
您的operator <
不符合std::map
的要求。特别是,您可以拥有一对Domain
,它们彼此都小于
考虑
Domain A("1", "2", 0);
Domain B("2", "1", 0);
std::cout << std::boolalpha << "A < B " << (A < B) << std::endl;
std::cout << std::boolalpha << "B < A " << (B < A) << std::endl;
从结构成员合成(总)顺序的最简单方法是将它们组装成std::tuple
,形成成员的词典顺序。
bool Domain::operator<( const Domain & other ) {
return std::tie(i, j, q) < std::tie(other.i, other.j, other.q);
}