You are not logged in. Please login at www.codechef.com to post your questions!

×

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<int,int>> > vsp;
for i=1 to n
  take set as local variable
  set<int,int> 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.

asked 04 Jun '17, 09:10

todumanish's gravatar image

4★todumanish
632
accept rate: 0%

edited 04 Jun '17, 09:20

What error are you getting?

(04 Jun '17, 09:14) vijju123 ♦♦5★

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

(04 Jun '17, 09:17) todumanish4★

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().

link

answered 04 Jun '17, 10:16

only4's gravatar image

4★only4
1.5k212
accept rate: 17%

edited 04 Jun '17, 18:36

1

right bro you are correct.

(04 Jun '17, 13:27) todumanish4★

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

link

answered 04 Jun '17, 09:18

hruday968's gravatar image

4★hruday968
1.7k210
accept rate: 14%

i did the same thing but i didn't mention here sry for that.

(04 Jun '17, 09:21) todumanish4★

Declare like this vsp[1000000]

(04 Jun '17, 09:22) hruday9684★

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;

(04 Jun '17, 09:26) todumanish4★

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;

(04 Jun '17, 09:28) todumanish4★

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

(04 Jun '17, 09:33) hruday9684★

@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).

link

answered 04 Jun '17, 09:33

vishesh_345's gravatar image

1★vishesh_345
2567
accept rate: 8%

edited 04 Jun '17, 09:40

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

(04 Jun '17, 09:43) todumanish4★

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?

(04 Jun '17, 09:51) vishesh_3451★

@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

vector<int>v; 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.

link

answered 04 Jun '17, 11:46

vishesh_345's gravatar image

1★vishesh_345
2567
accept rate: 8%

1

= 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.

(04 Jun '17, 11:51) only44★

"You can't assign value until the element is created" True. Thanks now it is clear.

(04 Jun '17, 12:14) vishesh_3451★

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;

link

answered 04 Jun '17, 16:21

divyansh_gaba7's gravatar image

4★divyansh_gaba7
76818
accept rate: 23%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×279
×163
×73

question asked: 04 Jun '17, 09:10

question was seen: 614 times

last updated: 04 Jun '17, 18:36