繁体   English   中英

如何打印元组矢量?

[英]How Do I Print a Vector of Tuples?

所以我试图制作一个包含两个整数的元组的向量,并且我从文本文件源获取整数。 为了确保我有我想要的矢量,我正在尝试打印我的内容,但输出没有显示任何东西。 我不确定是不是因为我的代码,以及我放置文本文件的地方。 我只是停留在这一点上。 如果有什么可以帮助我,我会非常感激。 谢谢

using namespace std;


int main()
{
ifstream file("source.txt");
typedef vector<tuple<int, int>> streets;
streets t; 
int a, b;

if (file.is_open()) 
{
    while (((file >> a).ignore() >> b).ignore())
    {
        t.push_back(tuple<int, int>(a, b));
        for (streets::const_iterator i = t.begin();i != t.end();++i)
        {
            cout << get<0>(*i) << endl;
            cout << get<1>(*i) << endl;
        }
        cout << get<0>(t[0]) << endl;
        cout << get<1>(t[1]) << endl;                   
    }
}

file.close();

system("pause");
return 0;

这是我的文本文件,我放置它的位置在这里输入图像描述

这是调试的输出,如果这很重要的话

你应该使用一个循环,一次打印一个元组。

完成最小的例子:

#include <iostream>
#include <tuple>
#include <vector>
#include <fstream>
using namespace std;

int main(void) {
    std::ifstream infile("source.txt");
    vector<tuple<int, int>> streets;
    int a, b;
    while (infile >> a >> b)
    {
        streets.push_back(tuple<int, int>(a, b));
    }
    infile.close();
    for(auto& tuple: streets) {
        cout << get<0>(tuple) << " " << get<1>(tuple) << endl;   
    }
    return 0;
}

输出:

1 2
3 4
5 6
7 8

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM