【问题标题】:c++ iterate through a vector of stringsc ++遍历字符串向量
【发布时间】:2016-10-25 08:17:36
【问题描述】:

所以我最近发现了 map 和 vector 的使用,但是,我很难找到一种方法来循环遍历包含字符串的向量。

这是我尝试过的:

#include <string>
#include <vector>
#include <stdio>

using namespace std;

void main() {
    vector<string> data={"Hello World!","Goodbye World!"};

    for (vector<string>::iterator t=data.begin(); t!=data.end(); ++t) {
        cout<<*t<<endl;
    }
}

当我尝试编译它时,我得到了这个错误:

cd C:\Users\Jason\Desktop\EXB\Win32
wmake -f C:\Users\Jason\Desktop\EXB\Win32\exbint.mk -h -e
wpp386 ..\Source\exbint.cpp -i="C:\WATCOM/h;C:\WATCOM/h/nt" -w4 -e25 -zq -od    -d2 -6r -bt=nt -fo=.obj -mf -xs -xr
..\Source\exbint.cpp(59): Error! E157: col(21) left expression must be integral
..\Source\exbint.cpp(59): Note! N717: col(21) left operand type is 'std::ostream watcall (lvalue)'
..\Source\exbint.cpp(59): Note! N718: col(21) right operand type is 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> (lvalue)'
Error(E42): Last command making (C:\Users\Jason\Desktop\EXB\Win32\exbint.obj) returned a bad status
Error(E02): Make execution terminated
Execution complete

我用 map 尝试了同样的方法,它奏效了。唯一的区别是我将 cout 行更改为:

cout<<t->first<<" => "<<t->last<<endl;

【问题讨论】:

  • 完全没问题。您的包含或编译器有问题。
  • 请提供minimal reproducible example。你最后错过#include &lt;string&gt;了吗?
  • 从错误文本判断,您的编译器出于某种原因将&lt;&lt; 视为位移。不过,您发布的那段代码很好,所以错误在其他地方。
  • 您的代码是合规的,并且在每个现代编译器上都能完美运行。我们帮不了你。

标签: c++ vector watcom


【解决方案1】:

添加iostream头文件并将stdio改为cstdio

#include <iostream>
#include <string>
#include <vector>
#include <cstdio>

using namespace std;

int main() 
{
    vector<string> data={"Hello World!","Goodbye World!"};
    for (vector<string>::iterator t=data.begin(); t!=data.end(); ++t) 
    {
        cout<<*t<<endl;
    }
    return 0;
}

【讨论】:

    【解决方案2】:

    来自Open Watcom V2 Fork-Wiki 上的C++ Library Status page

    大部分是完整的。虽然没有 I/O 操作符,但所有其他成员函数和字符串操作都可用。

    一种解决方法(除了实现&lt;&lt; 运算符之外)是向字符串实例询问 C 字符串:

    for (vector<string>::iterator t = data.begin(); t != data.end(); ++t) {
        cout << t->c_str() << endl;
    }
    

    这当然只适用于字符串不包含零字节值的情况。

    【讨论】:

      【解决方案3】:
      #include <iostream>
      #include <vector>
      #include <string>
       
      int main()
      {
         std::vector<std::string> data = {"Hello World!", "Goodbye World!"};
      
         for (std::vector<std::string>::iterator t = data.begin(); t != data.end(); t++) {
          std::cout << *t << std::endl;
         }
      
         return 0;
      }
      

      或使用 C++11(或更高版本):

      #include <iostream>
      #include <vector>
      #include <string>
      
      typedef std::vector<std::string> STRVEC;
      
      int main()
      {
          STRVEC data = {"Hello World!", "Goodbye World!"};
      
          for (auto &s: data) {
              std::cout << s << std::endl;
          }
      
          return 0;
      }
      

      【讨论】:

        【解决方案4】:

        当我编译你的代码时,我得到:

        40234801.cpp:3:17: fatal error: stdio: No such file or directory
         #include <stdio>
                         ^
        

        您的包含路径中显然有一个名为“stdio”的标头,但您没有向我们展示。

        如果您将该行更改为标准 #include &lt;iostream&gt;,则报告的唯一错误是您编写了 void main() 而不是 int main()。修复它,它就会构建并运行。

        顺便提一下,using namespace should be avoided.

        【讨论】:

          【解决方案5】:

          我找到了解决我自己问题的方法。我没有使用 c_str,而是使用 std::string 并切换到使用 G++ 编译器而不是 Open Watcom

          而不是:

          char *someString="Blah blah blah";
          

          我将其替换为:

          string someString="Blah blah blah";
          

          这种方式效率更高,也更容易。

          【讨论】:

          • 这根本没有回答这个问题。其实除了讲字符串之外,和这个问题完全没有关系。
          • 什么意思?它确实回答了我自己的问题,因为我的问题是编译错误,但正如我在回答中所说,问题源于编译器本身,所以我从 open watcom 切换到 G++。
          • 在您的原始问题中没有提到c_strchar*。所以你80-90%的答案是指不在问题中的东西。您使用 watcom 并且更改为 gcc 解决了问题的事实被所有其他信息所掩盖。
          • 是的,因为我说的是使用 c_str 或 char 不是我的问题所在。我相信它是,但我要说的是它是编译器的内部问题。我不确定你想在这里证明什么。
          • 不想证明什么。只是想让你理解你的问题和你的答案之间的差异。在您的问题中,绝对没有提到c_str。没有任何。一点痕迹都没有。没有。然后在您的回答中,您继续讨论c_strchar*string。您“认为问题出在c_str”这一事实完全无关,因为您没有在问题中写下它并且我们不介意读者。因此,尽管您的回答可能对您有意义,但对于只能阅读您的问题而不是您的想法的社区其他人来说,它没有。
          猜你喜欢
          • 2018-05-14
          • 1970-01-01
          • 1970-01-01
          • 2019-12-01
          • 2021-01-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-29
          相关资源
          最近更新 更多