使用标准C+;同时迭代std::vectors+; 我对C++是新手,因此在完成某项任务时需要一些帮助。问题是,我必须同时迭代三个或更多向量,如下所示: #include <vector> #include <iostream> #include <string> #include <boost/range/combine.hpp> using namespace std; int main(int, char**) { vector<string> questions = {"Planet", "Rocket", "Galaxy"}; vector<string> answers = {"Planet", "Saturn", "Star"}; vector<int> count = { 12, 34, 79}; vector<int> score = { 324, 956, 289}; vector<int> result; vector<int> subscore; string a, q; int c, s; for ( const string q : questions ) { int match = 0; for( auto tuple : boost::combine(answers, count) ) { boost::tie(a,c) = tuple; if( q.substr(0,2) == a.substr(0,2)) {std::cout << q.substr(0,3) << " " << a.substr(0,3) << endl; match = c; } else cout << "No match!" << '\n'; } if( match ) { result.push_back(match); } else result.push_back(0); subscore.push_back(0); #包括 #包括 #包括 #包括 使用名称空间std; int main(int,char**){ 向量问题={“行星”、“火箭”、“星系”}; 矢量答案={“行星”、“土星”、“恒星”}; 向量计数={12,34,79}; 向量得分={324956289}; 矢量结果; 向量子核; 字符串a,q; INTC,s; for(常量字符串q:问题){ int匹配=0; for(自动元组:boost::combine(答案、计数)){ boost::tie(a,c)=元组; 如果(q.substr(0,2)==a.substr(0,2)){std::cout

使用标准C+;同时迭代std::vectors+; 我对C++是新手,因此在完成某项任务时需要一些帮助。问题是,我必须同时迭代三个或更多向量,如下所示: #include <vector> #include <iostream> #include <string> #include <boost/range/combine.hpp> using namespace std; int main(int, char**) { vector<string> questions = {"Planet", "Rocket", "Galaxy"}; vector<string> answers = {"Planet", "Saturn", "Star"}; vector<int> count = { 12, 34, 79}; vector<int> score = { 324, 956, 289}; vector<int> result; vector<int> subscore; string a, q; int c, s; for ( const string q : questions ) { int match = 0; for( auto tuple : boost::combine(answers, count) ) { boost::tie(a,c) = tuple; if( q.substr(0,2) == a.substr(0,2)) {std::cout << q.substr(0,3) << " " << a.substr(0,3) << endl; match = c; } else cout << "No match!" << '\n'; } if( match ) { result.push_back(match); } else result.push_back(0); subscore.push_back(0); #包括 #包括 #包括 #包括 使用名称空间std; int main(int,char**){ 向量问题={“行星”、“火箭”、“星系”}; 矢量答案={“行星”、“土星”、“恒星”}; 向量计数={12,34,79}; 向量得分={324956289}; 矢量结果; 向量子核; 字符串a,q; INTC,s; for(常量字符串q:问题){ int匹配=0; for(自动元组:boost::combine(答案、计数)){ boost::tie(a,c)=元组; 如果(q.substr(0,2)==a.substr(0,2)){std::cout,c++,boost,stdvector,C++,Boost,Stdvector,您可以使用良好的旧索引: auto size = std::min( answers.size(), count.size() ); // or at least assert that size is equal for( size_t i = 0; i < size; ++i ) { const auto &a = answers[i]; const auto c = count[i]; // .. same as before auto size=std:

您可以使用良好的旧索引:

auto size = std::min( answers.size(), count.size() ); // or at least assert that size is equal
for( size_t i = 0; i < size; ++i ) {
   const auto &a = answers[i];
   const auto c = count[i];
   // .. same as before
auto size=std::min(answers.size(),count.size());//或者至少断言大小相等
对于(大小i=0;i

注意,这样你可能避免复制2个代码:<代码>:String 每一次迭代-Retry> Tuple > A< < /P> < P>这似乎是<代码>转换< /代码>,所以在C++中你可以使用<代码> STD::变换< /代码>…例如:

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main(int, char**) {
    vector<string> questions = {"Planet", "Rocket", "Galaxy"};
    vector<string> answers = {"Planet", "Saturn", "Star"};

    vector<int> count = { 12, 34, 79};

    vector<int> result;

    for(const auto& q : questions)
    {
        transform(begin(answers), end(answers), begin(count), back_inserter(result),
                  [&q](const auto& answer, auto count)
        {
            if (q.substr(0, 2) == answer.substr(0, 2))
            {
                std::cout << q.substr(0,3) << " " << answer.substr(0,3) << endl;
                return count;
            }
            else
                cout << "No Match!" << endl;

            return 0;
        });
    }
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int,char**){
向量问题={“行星”、“火箭”、“星系”};
矢量答案={“行星”、“土星”、“恒星”};
向量计数={12,34,79};
矢量结果;
用于(const auto&q:问题)
{
转换(开始(答案)、结束(答案)、开始(计数)、返回插入器(结果),
[&q](常数自动和应答,自动计数)
{
如果(q.substr(0,2)=答案.substr(0,2))
{

std::cout为什么不能在框架中使用boost?使用旧的良好索引还不够花哨?使用根数据分析框架。默认情况下,它不知道boost。因此,使用标准C++可能是更好的方法。框架不会将您限制在它“知道”的范围内,或者它不是一个框架,或者更多的是一个障碍。这很好,非常感谢您的支持。