Skip to main content
gamedev.net
🔒 Locked

How do iterators work

Started by Sagar_Indurkhya Jul 11, 2004 at 4:05 PM 2 replies 300+ views
Original Post
Sagar_Indurkhya
Sagar_Indurkhya
I have this code: Assumptions - 1) data is a vector filled with strings from a file. 2) the first string is an int.


//convert string to int 
int x;
stringstream StrToInt(data[0]);
StrToInt >> x;

cout << x << "\n";

x turns out to be some number like -834039483, when it should be 5. why isn't this working. Do I need to use an iterator?
smart_idiot
smart_idiot
An iterator is like a position in something; and they're used almost the same way as a pointer.

Example:

#include <iostream>#include <string>using namespace std;int main() {  string word("Hello.");   for(string::iterator character = word.begin(); character != word.end(); ++character)   cout << *character << endl;    return 0; }


That goes through each character in the string and prints it.

As for what you're doing; it has nothing to do with iterators. It kind of looks like data[0] is empty and nothing was assigned to x, leaving it filled with garbage. Try printing the value of data[0] to make sure it actually contains what you think it does.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
Sagar_Indurkhya
Sagar_Indurkhya
The first line of the data is suppose to be a 5.
However, when I print the entire contents of my vector, it prints everything after the first line.
Chris Hare
Chris Hare
int main(){	const char* c = "5";	stringstream StrToInt;	StrToInt << c;	int x = 0;	StrToInt >> x;	cout << x << "\n";	return 0;}


???

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.