【问题标题】:Filling vector-of-pairs with pair-of-pair not working as expected用对填充向量对没有按预期工作
【发布时间】:2018-10-20 03:14:29
【问题描述】:

我有以下代码填充包含对的向量。

std::vector<std::pair<double, std::pair<int, int>>> vec;
int x=100, y=10, z=20;
vec.push_back(std::make_pair((double)x,std::make_pair(y,z)));
for(int i=0;i<vec.size();i++){
    std::cout<<"x: "<<vec[i].first<<"\n";
    std::cout<<"y: "<<vec[i].second.first<<"\n";
    std::cout<<"z: "<<vec[i].second.second<<"\n";
}

输出:

x: 0
y: 0
z: 0

为什么不打印如下?

x: 100.0
y: 10
z: 20

【问题讨论】:

    标签: c++ vector std-pair keyvaluepair


    【解决方案1】:

    它按预期工作。使用 G++ 编译器测试。 未发现任何问题。

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        // your code goes here
        std::vector<std::pair<double, std::pair<int, int>>> vec;
        int x = 100, y = 10, z = 20;
        vec.push_back(std::make_pair((double)x, std::make_pair(y, z)));
        for (int i = 0; i < vec.size(); i++) {
            std::cout << "x: " << vec[i].first << "\n";
            std::cout << "y: " << vec[i].second.first << "\n";
            std::cout << "z: " << vec[i].second.second << "\n";
        }
    
        return 0;
    }
    

    请参考this链接

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      • 1970-01-01
      • 2015-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-18
      相关资源
      最近更新 更多