简体   繁体   English

如何初始化配对 <vector<pair<bool,int> &gt;,向量 <pair<bool,int> &gt;&gt;在C ++ 11中

[英]How to initialize pair<vector<pair<bool,int>>,vector<pair<bool,int>>> in C++11

I'd like to initialize: 我想初始化:

pair<vector<pair<bool,int>>,vector<pair<bool,int>>> pvp;

so that for all i: 这样,对于我来说:

pvp.first[i].first = true;

and

pvp.second[i].first = false;

I know that you could do this with a loop but isn't there any faster way like an initialization for a vector ? 我知道您可以通过循环执行此操作,但是没有像矢量初始化那样的更快方法吗?

Sorry, I do not have a direct answer to the question, but I do not see the the question as the real problem. 抱歉,我没有直接回答这个问题,但是我没有将该问题视为真正的问题。

Generic data structures are great, but maybe, consider a few classes, instead. 通用数据结构很棒,但也许可以考虑考虑几个类。 That way the individual class constructers can handle the initializations as needed (in smaller pieces). 这样,各个类的构造函数就可以根据需要(较小的片段)处理初始化。

The initialization syntax would be: 初始化语法为:

pvp{ vector<pair<bool, int>>(5, { true, 0 }), vector<pair<bool, int>>(5, { false, 0 }) };

Now, you didn't specify any length of the array (or what the integer should be), but here's the full approach: 现在,您没有指定数组的任何长度(或整数应该是什么),但这是完整的方法:

#include <tuple>
#include <vector>
#include <iostream>

using namespace std;

int main(){

    pair<vector<pair<bool, int>>, vector<pair<bool, int>>> pvp{ vector<pair<bool, int>>(5, { true, 0 }), vector<pair<bool, int>>(5, { false, 0 }) };

    for (auto i : pvp.first){
        cout << (i.first ? "true" : "false") << '\n';
    }
    for (auto i : pvp.second){
        cout << (i.first ? "true" : "false") << '\n';
    }

    return 0;
}

Output: 输出:

 true
 true
 true
 true
 true
 false
 false
 false
 false
 false

As already mentioned, this implementation is too complex for a simple human reader to understand. 如前所述,这种实现对于一个简单的人类读者来说太复杂了。 Separate it into smaller pieces, though. 但是,将其分成较小的部分。

Consider using typedefs to make the code easier to read. 考虑使用typedef使代码更易于阅读。

using MyPair = pair<bool,int>;
using MyPairs = vector<MyPair>;
pair<MyPairs,MyPairs> pvp{MyPairs{make_pair(true,10)},
                          MyPairs{make_pair(false,11)}};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 初始化(bitset,int)对的向量 - initialize vector of pair of (bitset,int) 我如何声明和初始化这个地图<pair<int, int> ,布尔&gt;? - How do I declare and initialize this map<pair<int, int>, bool>? C++:矢量<pair<vector<int> ,int&gt;&gt; </pair<vector<int> - C++: vector <pair<vector<int>,int> > 具有两个GLfloats作为对的C ++ 11向量无法统一初始化 - C++11 vector with two GLfloats as pair fail to uniform initialize C ++如何在vector&lt;对中找到一对3个整数<int, pair<int, int> &gt; &gt; - C++ How to find pair of 3 integers in vector< pair<int, pair<int, int> > > 如何对vector &lt;pair &lt;int,pair进行排序 <int , pair<string , pair<int , int > &gt;&gt;&gt;&gt;? - How to sort vector< pair< int , pair<int , pair<string , pair<int , int > > > > >? 向量将如何进行排序 <pair<int,pair<int,int> &gt;&gt;? - How will the sorting be done with vector<pair<int,pair<int,int>>>? 为什么这个声明`priority_queue <pair<int,int> , 向量<pair<int,int> &gt;,compare&gt;pq;` 在 C++03 中是错误但在 C++11 中正确吗? - Why this statement `priority_queue <pair<int,int>, vector<pair<int,int> >,compare>pq;` is an error in C++03 but correct in C++11? 矢量是怎样的<vector<int> > 比向量“更重” <pair<int,int> >? </pair<int,int></vector<int> - How is vector<vector<int>> "heavier" than vector<pair<int,int>>? 打印一个`向量<pair<int, int> &gt;` - Printing a `vector<pair<int, int>>`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM