Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ how to get ONLY integers from complex string

I have few strings, each one contains one word and several integer numbers (One string is whole line):

Adam 2 5 1 5 3 4
John 1 4 2 5 22 7
Kate 7 3 4 2 1 15
Bill 2222 2 22 11 111

As you can see, each word/number is separated with space. Now, I want to load these data into a map, where word (name) would be the key and the value would be vector of the numbers in line. I already have key values in separated temporary stl container, so the task is to load only the integer numbers from each line to 2D vector and then merge these two into map. The question is, is there any C++ function, which would avoid words and white spaces and get only integers from a string, or I have to search strings char-by-char like here ?

I found only partial solution, which is not able to get more than one digit number:

vector<int> out;

for (int i = 0; i < line.length(); i++) {

    if (isdigit(line.at(i))) {  
        stringstream const_char;
        int intValue;
        const_char << line.at(i); 
        const_char >> intValue;
        out.push_back(intValue);
    }
}
like image 737
witcher Avatar asked Apr 10 '26 23:04

witcher


2 Answers

If every line has the format "word number number number ...", use a stringstream and skip the word by reading it.

If the current line is in line:

vector<int> out;
istringstream in(line);
string word;
in >> word;
int x = 0;
while (in >> x)
{
    out.push_back(x);
}
like image 195
molbdnilo Avatar answered Apr 13 '26 12:04

molbdnilo


split the string on spaces since that seems to be your delimiter. Then check that each substring contains an int with strol.

Then use stoi to convert the integer substrings to int.

If no stoi conversion can be performed (the string does not contain a number), an invalid_argument exception is thrown, so don't try to convert the name substring.

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

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}


std::vector<std::string> split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    split(s, delim, elems);
    return elems;
}

inline bool isInteger(const std::string & s)
{
   if(s.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) return false ;

   char * p ;
   strtol(s.c_str(), &p, 10) ;

   return (*p == 0) ;
}


int main()
{
   std::cout << "Hello World" << std::endl;

   std::string example="Adam 2 5 1 5 3 4";

   std::vector<std::string> subStrings;

   subStrings = split(example, ' ');

   std::string sItem;
   for(std::vector<std::string>::iterator it = subStrings.begin(); it != subStrings.end(); ++it) {
        sItem = *it;
        if( isInteger(sItem) ){
            int nItem = std::stoi (sItem);
            std::cout << nItem << '\n';
        }
    }

   return 0;
}
like image 28
ldgorman Avatar answered Apr 13 '26 13:04

ldgorman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!