0

I want to insert values into a vector of set<int>, which is defined using typedef as given below:

typedef std::set<int> blockSet_t;
std::vector<blockSet_t>* myvector

I want to have values such that myvector[0] has a set of ints, myvector[1] has a different set of ints and so on.

Currently, I am passing this vector to a function which is parsing a file that has the set of integers.

Like:

main()
{
 std::vector<blockSet_t> myvector;
 filereader(myvector);
}

I read the set from the file and store it in a different blockSet_t myset;

I am using the following loop to store this set to a specific location in the vector:

filereader(&myvector)
{
 for(int i=0;i<size;i++)
 {
  myvector.push_back(myset); // It does not give error but I don't know myset is stored in which location
//what I want is to have someting like this

  myvector[i].push_back(myset); //so I can store different sets at different locations
 }
}

I also could not figure out, how to display the values from within thevector.

Since it is a vector of sets, I want to display each set (on a different vector index).

Any help in this regard is much appreciated.

Thanks.

Ahmed Akhtar
  • 1,444
  • 1
  • 16
  • 28
  • It's a little unclear what you're asking. Given a set, you want to push that set into a vector of sets? You also want to be able to iterate through that same vector and print each element in each set? Is that your question? – NickLamp Feb 20 '16 at 04:03
  • Why use a `vector` then? Why not use a dynamic array of `set`s? – Ahmed Akhtar Feb 20 '16 at 04:07

1 Answers1

1

First of all, push_back function is called push_back because it pushes an object back.

It means that, if your vector is empty and you call push_back your pushed object will have 0 location.

If your vector has n objects inside it, this means after push_back your pushed object will have n-th index.

myvector.push_back(myset);
std::cout<<"index of myset is "<<myvector.size()-1<<std::endl;

Secondly, if you want to print values, you have to create your own operator<< overloading function for std::ostream class. This is a common way of printing values in C++.

Assume you want to print set in curly {} brakets and vector in square [] ones:

#include <ostream>

inline std::ostream& operator<<(std::ostream &os,const blockSet_t &mySet)
{
    os<<"{ ";
    for(const auto &value:mySet)
    {
        os<<value<<' ';
    }
    os<<"};
    return os;
}

inline std::ostream& operator<<(std::ostream &os,const std::vector<blockSet_t> &myvector)
{
    os<<"[ ";
    for(const auto &mySet:myvector)
    {
        os<<mySet<<' ';
    }
    os<<"];
    return os;
}

Next, you have to cout your object like this:

#include <ostream>

main()
{
    std::vector<blockSet_t> myvector;
    filereader(myvector);
    std::cout<<myvector<<std::endl;
}
Ahmed Akhtar
  • 1,444
  • 1
  • 16
  • 28
fnc12
  • 2,241
  • 1
  • 21
  • 27
  • Thanks you, So when I push_back myvector.push_back(myset1), if my vector is empty the myse1t goes to 0 location. and when I do myvector.push_back(myset2) again it goes to 1 location right? Now when I display the Size of each set using something like , for(int i=0;i – Syed Aftab Rashid Feb 20 '16 at 15:07
  • @SyedAftabRashid give me your code. Or contact me using skype fnc1234 and we will solve your problem – fnc12 Feb 20 '16 at 15:12