1

I am reading coordinates from a text file. For example "0 50 100". I am keeping my text file in a string vector. I want to get 0 and 50 and 100 seperately. I thought that I can make it as getting a string between 2 spaces then convert it to integer using stoi. But I couldn't achieve to get a string between 2 spaces seperately. I shared what I've tried. I know that this is not correct. Can you please help me to find my solution?

Sample text input: Saloon 4 0 0 50 0 50 100 0 100. (4 means that saloon has 4 points. Ex: First two integer after 4 shows (x1,y1))

    for (int j = 2; j < n * 2 + 2; j++){
            size_t pos = apartmentInfo[i].find(" ");
            a = stoi(apartmentInfo[i].substr(pos + j+1,2));
            cout << "PLS" << a<<endl;
        }
codelife
  • 17
  • 6

2 Answers2

0

You can use std::istringstream to extract the integers from the text:

#include <sstream>
#include <vector>
#include <string>
#include <iostream>

int main()
{
   std::string test = "0 50 100";
   std::istringstream iss(test);

   // read in values into our vector
   std::vector<int> values;
   int oneValue;
   while (iss >> oneValue )
     values.push_back(oneValue);

   // output results
   for(auto& v : values)
     std::cout << v << "\n";
}

Live Example


Edit: Reading name, number, and then the integers:

#include <sstream>
#include <vector>
#include <string>
#include <iostream>

int main()
{
   std::string test = "Saloon 4 0 0 50 0 50 100 0 100";
   std::string name;
   int num;
   std::istringstream iss(test);
   iss >> name;
   iss >> num;
   std::cout << "Name is " << name << ".  Value is " << num << "\n";
   std::vector<int> values;
   int oneValue;
   while (iss >> oneValue )
     values.push_back(oneValue);
   std::cout << "The values in vector are:\n";
   for(auto& v : values)
     std::cout << v << " ";
}

Live Example 2

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
0

Parsing numbers from input streams such as std::ifstream is easy in the normal case, as the streams already have the necessary parsing capabilities.

E.g., to parse an int from a file-input stream:

std::ifstream in{"my_file.txt"};
int number;
in >> number; // Read a token from the stream and parse it to 'int'.

Let's say you have an aggregate class coord containing the x and y coordinates.

struct coord {
    int x, y;
};

You can add custom parse behaviour for the class coord so that it can read both x and y values at the same time, when parsing into it from an input stream.

std::istream& operator>>(std::istream& in, coord& c) {
    return in >> c.x >> c.y; // Read two times consecutively from the stream.
}

Now all the tools in the standard library that uses streams can parse coord objects. E.g.:

std::string type;
int nbr_coords;
std::vector<coord> coords;

if (in >> type >> nbr_coords) {
    std::copy_n(std::istream_iterator<coord>{in}, nbr_coords, std::back_inserter(coords));
}

This will read and parse the correct number of coord objects into a vector, each coord object containing both an x and a y coordinate.

Live example

Extended live example

Felix Glas
  • 15,065
  • 7
  • 53
  • 82