在集合中< int,vector< int>> >如何在集合中已经存在的向量中添加另一个元素

发布于 2025-02-08 11:47:34 字数 1431 浏览 0 评论 0原文

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文