query related to stl vector and set

 can we assign a set to each index of vector. if it is possible how can we do implement this?
i did like this :
vector<set<pair> > vsp;
for i=1 to n
  take set as local variable
  set s;
  insert some values in s;
  and assign this set to vector
  vsp[i]=s;
but it is giving error.what should i do any help?thanks in advance.

You are using set pair,so syntax should be like this vector<set<pair<int,int> > >vsp

@todumanish you have declared set<int,int> s;, I guess this should be like this set<pair<int,int> >s. I donā€™t know whether this is the mistake but if we assign v[i]=s where s is set<int,int> that would give a error(mismatch).

You can do that. Take a look.

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <stdio.h>
#include <cstring>

using namespace std;

vector<set<pair<int, int>>> vsp;
set<pair<int,int>> s;

int main()
{
    int t;
    //cin >> t;
    t = 5;
    for (int i = 0; i < t; i++)
    {
        for (int j = 0; j < i + 1; j++)
        {
            s.insert(make_pair(j, 2 * j));
        }
    }
    vsp.push_back(s);
    for (auto it : vsp[0])
    {
        cout << it.first << " " << it.second << endl;
    }
}

I think the problem is in vsp[i]=s part, (as iā€™th element doesnā€™t exist) you must use push_back().

3 Likes

@todumanish I tried using v[i] for insertion but it showed an error. This might be the reason that when you assign v[i] to some set (like v[i]=s instead of v.push_back(s)) compiler misunderstand v[i] as an array of vectors and we are assigning something to an element of that means we are assigning a value to a vector (which surely gives and error )

This is same as writing

vectorv; and then v=3;

So it is better to use push_back as @only4 has mentioned in his code.
Does this make sense? Please correct if wrong.

change vector < set < pair< int,int > > > vsp; to vector < set < pair < int,int > > > vsp(n);

and set < int,int > s; to set < pair< int,int > > s;

What error are you getting?

is this correct. error is type mismatched in last line vsp[i] = s

i did the same thing but i didnā€™t mention here sry for that.

Declare like this vsp[1000000]

i already did the same thing. error is type mismatch.error: no match for ā€˜operator=ā€™ (operand types are ā€˜std::vector<std::set<std::pair<int, int> > >ā€™ and ā€˜std::set<std::pair<int, int> >ā€™)
v[i] = s;

error: no match for ā€˜operator=ā€™ (operand types are ā€˜std::vector<std::set<std::pair<int, int> > >ā€™ and ā€˜std::set<std::pair<int, int> >ā€™)
v[i] = s;

After declaring,if you want to assign a set s to v[i] just write v[i]=s

thanks for telling me but i already did the same i already edit my question.

Can we assign like this in vectors. I mean v[i] is used to random access the vector elements but I never used it during insertion (i may be wrong). Have you tried push_back or it is required to insert them on specific positions?

= is assignment operator. Read http://www.cplusplus.com/reference/vector/vector/push_back/

push_back() adds a new element. You canā€™t assign value until the element is created.

1 Like

ā€œYou canā€™t assign value until the element is createdā€ True. Thanks now it is clear.

right bro you are correct.

1 Like