[英]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.