我必须打印一对的值。为了显示该对的第一个值,没有任何问题。但我如何打印该对的第二个值? 表示无法更改。
typedef vector<char> _vots;
typedef pair<string,_vots> PartitPolitic;
ostream& operator<<(ostream &o, PartitPolitic x){
o << x.first << endl;
o << x.second << endl;->>>>>>>>>>>>>>>>> ERROR
return o;
}
int main(){
vector<PartitPolitic> partit;
string q;
string s;
getline(cin,descripcio);
while (q!="*"){
getline(cin,s)
_vots v(s.begin(),s.end());
PartitPolitic f(descripcio,v);
partit.push_back(f);
getline(cin,descripcio);
}
vector<PartitPolitic>::iterator it =partit.begin();
while(it!=partit.end()){
cout << *it << endl;
it++;
}
return 0;
}
答案 0 :(得分:2)
我如何打印该对的第二个值?
第二个值是vector<char>
,因此要打印您需要为operator<<
提供vector
重载,定义您希望如何打印矢量:
template<typename elem_type>
ostream& operator<<(ostream &o, vector<elem_type> const& v) {
for (const elem_type& e : v)
o << e << ",";
return o;
}
或者您只需在operator<<(ostream&, PartitPolitic)
中手动输出矢量:
ostream& operator<<(ostream &o, PartitPolitic x) {
o << x.first << endl;
for (char e : x.second)
o << e << ",";
return o;
}