【发布时间】: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 <string>了吗? -
从错误文本判断,您的编译器出于某种原因将
<<视为位移。不过,您发布的那段代码很好,所以错误在其他地方。 -
您的代码是合规的,并且在每个现代编译器上都能完美运行。我们帮不了你。