[英]C++ : iterate through a vector of set of vectors
I want something like this: 我想要这样的东西:
{ {0,1},{0,2},{0,3},{0,4}...... {255,255} } ---> (1)
After some processing I have to get something like this: 经过一些处理后,我必须得到这样的东西:
{ {0,1},{0,2},{0,3} }
{ {0,4} }
{ {5,6},{255,255} }
Thus, initially one big set consists of a number of subsets. 因此,最初一个大集包含许多子集。 Each subset consists of two values. 每个子集由两个值组成。 After some processing the one big set in (1) decomposes like above. 经过一些处理后,(1)中的一个大集合如上所述进行分解。
Now I want to iterate through the set in (1) and get each value from each subset comprising of two elements. 现在我想迭代(1)中的集合并从包含两个元素的每个子集中获取每个值。
Here is my code. 这是我的代码。 Please guide me how would I extract subset values. 请指导我如何提取子集值。
CrtBestPartitionDouble corresponds to a big set consisting of subsets. CrtBestPartitionDouble对应于由子集组成的大集合。 subgroup is a subset comprising of two elemets doublegroup is a set consisiting of all the subsets 子组是包含两个elemets的一个子集是doublegroup所有子集的一组consisiting
vector<set<vector<int> > > CrtBestPartitionDouble;
vector<set<vector<int> > > ::iterator itr;
set<vector<int> > doublegroup;
set<vector<int> >::iterator itr2;
vector<int>::iterator itr3 ;
for(int k=0; k <5; k++)
{
for(int j=0; j <5; j++)
{ vector<int> subgroup;
subgroup.push_back(k);
subgroup.push_back(j);
doublegroup.insert(subgroup);
}
}
CrtBestPartitionDouble.push_back(doublegroup);
for ( itr = CrtBestPartitionDouble.begin(); itr != CrtBestPartitionDouble.end(); ++itr ) {
// Iterate each set
for ( itr2 = itr->begin(); itr2 != itr->end(); ++itr2 ) {
// Iterate each subset
for ( itr3 = itr2->begin(); itr3 != itr2->end(); ++itr3 ) {
std::cout << *itr3;
}
}
}
It gives me following error: 它给了我以下错误:
ST.cpp: In function ‘void StateTableGenerator()’:
ST.cpp:460:39: error: no match for ‘operator=’ in ‘itr3 = itr2.
you can use this code to iterate over all values, I'am not sure if this is what you want 您可以使用此代码迭代所有值,我不确定这是否是您想要的
typedef vector<set<set<int> > > CrtBestPartitionDoubleType;
CrtBestPartitionDoubleType CrtBestPartitionDouble;
// Iterate each vector element
for ( CrtBestPartitionDoubleType::iterator itr = CrtBestPartitionDouble.begin(); itr != CrtBestPartitionDouble.end(); ++itr ) {
// Iterate each set
for ( set<set<int> >::iterator itr2 = itr->begin(); itr2 != itr->end(); ++itr2 ) {
// Iterate each subset
for ( set<int>::iterator itr3 = itr2->begin(); itr3 != itr2->end(); ++itr3 ) {
std::cout << *itr3;
}
}
}
With range based for: 范围基于:
for(const auto& a: CrtBestPartitionDouble))
{
for(const auto& b: a)
{
for(const auto& c: b)
std::cout << c;
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.