So I recently discovered the use of map and vectors, however, I'm having trouble of trying to figure a way to loop through a vector containing strings.
Here's what I've tried:
#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;
}
}
and when I try to compile it, I get this error:
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
I tried the same method using map and it worked. The only difference was I changed the cout line to:
cout<<t->first<<" => "<<t->last<<endl;
#include <string>
eventually? – Waggery<<
as a bitshift for some reason. The bit of code that you posted is fine, though, so the error is somewhere else. – Oswaldooswalt#include <iostream>
. Check the references. – Supposed#include <iostream>
and change the return type ofmain
toint
. – Unicuspidmap
example worked for you, try to find out how you would access the element your iterating over int
. – Supposedvoid main
and#include <stdio>
are not C++! It must beint main
and#include <iostream>
. What's true is that your compiler is the culprit and that you should use a modern alternative like the latest VC, GCC or Clang. – Mcculleyfor (const auto& s : data) { std::cout << s << std::endl; }
– Rick#include <iostream>
). Your problem is the compiler. Watcom is beyond salvation. Open-sourcing a 1999 product is too little, too late. Give it up. Use MSVC for DOS and Windows and GCC for Linux, OS/2 and netware. – Biondo