在集合中< int,vector< int>> >如何在集合中已经存在的向量中添加另一个元素
#include <bits/stdc++.h>
using namespace std;
typedef pair <int , vector<int> > pairs;
void solve () {
set <pairs> s ;
for ( int i =0; i<5; i++){
vector<int> v ;
for ( int j = i; j<=i + i; j++){
v.push_back(j) ;
}
s.insert ({i,v}) ;
}
// printing values of set
for ( auto &x : s) {
cout<<x.first<<": ";
for ( auto i : x.second)
cout<<i<<" " ;
cout<<endl;
}
// searching for this <int,vector> pair in set
// to modify it
int val = 2 ;
vector<int> vf = {2,3,4} ;
auto it = s.find ( {val, vf} ) ;
if ( it != s.end() ) {
// printing some random stuff about this set element
cout<<(*it).first<<endl ;
cout<<(*it).second.size()<<endl ;
// inserting another value at the end of this vector
(*it).second.push_back(10001);
}
}
int main () {
solve() ;
return 0;
}
因此,在此代码中,我无法修改一对&lt的集中的向量; int,vector&lt; int&gt; &gt;
我发现这些代码行的工作正常:
cout<<(*it).first<<endl ;
cout<<(*it).second.size()<<endl ;
但是我面临错误以执行此行:
(*it).second.push_back(10001);
为什么push_back()操作在这里不起作用? 有什么简单的修复方法吗?
#include <bits/stdc++.h>
using namespace std;
typedef pair <int , vector<int> > pairs;
void solve () {
set <pairs> s ;
for ( int i =0; i<5; i++){
vector<int> v ;
for ( int j = i; j<=i + i; j++){
v.push_back(j) ;
}
s.insert ({i,v}) ;
}
// printing values of set
for ( auto &x : s) {
cout<<x.first<<": ";
for ( auto i : x.second)
cout<<i<<" " ;
cout<<endl;
}
// searching for this <int,vector> pair in set
// to modify it
int val = 2 ;
vector<int> vf = {2,3,4} ;
auto it = s.find ( {val, vf} ) ;
if ( it != s.end() ) {
// printing some random stuff about this set element
cout<<(*it).first<<endl ;
cout<<(*it).second.size()<<endl ;
// inserting another value at the end of this vector
(*it).second.push_back(10001);
}
}
int main () {
solve() ;
return 0;
}
So in this code i m unable to modify the vector present in a set of pair of < int, vector<int> >
I found out that these lines of code works fine :
cout<<(*it).first<<endl ;
cout<<(*it).second.size()<<endl ;
but I m facing error to execute this line :
(*it).second.push_back(10001);
Why isn't push_back() operation not working here ?
Any easy way to fix it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论