变量在迭代时不打印 [英] Variable not printing on iteration

查看:140
本文介绍了变量在迭代时不打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为个人项目编写了一个C ++ CSV类,但却发现了一个奇怪的错误。在我的测试程序我有代码:

I'm writing a C++ CSV class for a personal project, but have stumbled on a weird error. In my test program I have the code:

for( int i = 0; i < 3; ++i ) {
    std::cout << i << ": ";
    std::vector<std::string> results = test.get_row();
    for( auto it = results.begin(); it != results.end(); ++it ) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;
}

并获得结果:

 : row0 row1 row2
 : blah0 blah1 blah2
2: blah3 blah4 blah5

但我应该得到:

0: row0 row1 row2
1: blah0 blah1 blah2
2: blah3 blah4 blah5

我在前两次运行时没有得到 i 值?

Any ideas as to why I'm not getting the i value on the first two runs?

注意:我使用g ++ 4.4 .7

Note: I'm using g++ 4.4.7

推荐答案

啊。我想我看到了:

您似乎忘记了从输入中删除换行符。

You appear to have forgotten to strip the newlines from the input.

这将导致最后一列具有尾随'\r',并且由于始终添加一个覆盖最初打印的 i

This would resulting in the last column having a trailing '\r', and since you always add a ' ', this space will overwrite the originally printed i.

在拆分列值之前从输入中剥离换行符:

Strip the newline character from the input before splitting columns values :)

这篇关于变量在迭代时不打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆