Consider the code:
void someFunc {
std::stringstream value;
std::vector<std::vector<int>> mapLayerCollision;
int row = 0;
for(int i = 0; i < gid_list.length(); i ) {
if(gid_list[i] == ',') {
value.str("");
int j = 1;
while (gid_list[i-j] != ',' and i - j > 0) {
std::cout << gid_list[i-j] << " | ";
j ;
std::cout << gid_list[i-j] << std::endl;
}
j--;
value.str("");
std::cout << "j: " << j << std::endl;
while (j > 0) {
std::cout << "Inserting: " << gid_list[i-j] << std::endl;
value << gid_list[i-j];
std::cout << "Value AFTER: " << value.str() << std::endl;
j--;
}
std::cout << "Putting into vector: " << std::stoi(value.str()) << std::endl;
//mapLayerCollision[row].push_back(std::stoi(value.str()));
value.str("");
}
else if(gid_list[i] == '\n') {
value.str("");
int j = 1;
while (gid_list[i-j] != ',') {
j ;
}
j--;
value.str("");
while (j > 0) {
value << gid_list[i-j];
j--;
}
std::cout << "Putting into vector: " << value.str() << std::endl;
//mapLayerCollision[row].push_back(std::stoi(value.str()));
value.str("");
row ;
}
}
}
The declaration of gid_list
is: std::string gid_list;
, and the contents are:
79,454,454,454,454,454,454,454,454,454,454,454,454,454,454,454,454,454,454,454,454,454,454,455,70,
274,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,259,
274,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,259,
274,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,259,
274,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,195,210,0,0,0,0,0,259,
274,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,259,274,0,0,0,0,0,259,
274,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,323,338,0,0,0,0,0,259,
274,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,387,402,0,0,0,0,0,259,
274,0,0,346,0,0,0,350,0,0,0,0,0,0,0,0,0,451,466,0,0,0,0,0,259,
274,0,0,410,0,0,0,414,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,259,
274,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,259,
338,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,259,
402,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,259,
338,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,195,210,0,0,0,0,0,0,259,
402,0,0,0,0,195,198,199,200,210,0,0,0,0,0,0,451,466,0,0,0,0,0,0,259,
274,0,0,0,0,387,264,264,264,402,0,0,0,0,0,0,0,0,0,0,0,0,0,0,259,
274,0,0,0,0,451,455,456,457,466,0,0,0,0,0,0,0,0,0,0,0,0,0,0,259,
274,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,259,
212,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,216,
73,198,198,198,198,198,198,198,198,198,198,198,198,198,198,198,198,198,199,200,201,199,200,201,76
So I have the lines of code that insert the stringstream value into the vector commented out, the values being printed are correct, but as soon as I uncomment those lines the printed values of the stringstream are garbled and seem to be filled with random stuff. I know that stringstreams can be finicky, but the most confusing part is that the values are printed BEFORE being added to the vector. Question: Why is the stringstream value changing when a line of code is modified AFTER it, and how can I fix it?
CodePudding user response:
I know that
stringstream
can be finicky
It is not finicky, you use it improperly.
All your code can be simpler
std::vector<std::vector<int>> mapLayerCollision;
std::string line;
while (std::getline(cin, line)) {
mapLayerCollision.emplace_back(); // This fixes your issue you have asked
std::istringstream values(line);
int n;
while (values >> n) {
mapLayerCollision.back().push_back(n);
char c;
if ((values >> c) && c != ',') {
// bad content
break;
}
}
}
The issue in your code is mapLayerCollision[row]
that attempts to access row
th element of the empty vector.
CodePudding user response:
I fixed my issue by adding this->mapLayerCollision.emplace_back();
to every while
loop. Revised code:
void someFunc {
std::stringstream value;
std::vector<std::vector<int>> mapLayerCollision;
int row = 0;
for(int i = 0; i < gid_list.length(); i ) {
if(gid_list[i] == ',') {
value.str("");
int j = 1;
while (gid_list[i-j] != ',' and i - j > 0) {
this->mapLayerCollision.emplace_back();
j ;
}
j--;
value.str("");
std::cout << "j: " << j << std::endl;
while (j > 0) {
this->mapLayerCollision.emplace_back();
value << gid_list[i-j];
j--;
}
mapLayerCollision[row].push_back(std::stoi(value.str()));
value.str("");
}
else if(gid_list[i] == '\n') {
value.str("");
int j = 1;
while (gid_list[i-j] != ',') {
j ;
}
j--;
value.str("");
while (j > 0) {
this->mapLayerCollision.emplace_back();
value << gid_list[i-j];
j--;
}
value.str("");
row ;
}
}
}