如何向vector c ++添加一个新集合

时间:2016-10-01 07:11:00

标签: c++

我正在尝试向我的向量添加一个新集合,这是我的代码

set<string>::iterator iter;
vector<set<string> > myVector;
set<string> mySet;

mySet.insert("hello");
myVector.push_back(mySet);
mySet.clear();
mySet.insert("world");
myVector.push_back(mySet);

cout << mySet.size() << endl;
cout << myVector.size() << endl;
// printing out vector of sets
for (int i = 0; i < myVector.size(); i++)
{
    for (iter = mySet.begin(); iter != mySet.end(); ++iter)
    {
        cout << *iter << " ";
    }
    cout << endl;
}

输出

    1
    2
    world
    world

我打印或实施不正确吗?我想要它打印

    1
    2
    hello
    world

1 个答案:

答案 0 :(得分:0)

首先,您不打印矢量值

for (int i = 0; i < myVector.size(); i++)
{
    for (iter = myVector[i].begin(); iter != myVector[i].end(); ++iter)
    {
        cout << *iter << " ";
    }
    cout << endl;
}