粗大メモ置き場

個人用,たまーに来訪者を意識する雑記メモ

How to split string using multiple delimeters / and convert to double array in C++

GOAL: get double array from "[1.1, 3.3]"

Unfortunately std::string do not have split function.

I've heard boost/ library has split function but I do not want to use that.

Split string into string vector

Here is sample code.
The original code is from below:
c++ - Split a string into words by multiple delimiters - Stack Overflow

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

int main(){
std::string str = "[0.0, 2.1, 22.3]";
   std::vector<std::string> v ; //Use vector to add the words

    std::size_t prev_pos = 0, pos;
    while ((pos = str.find_first_of("[] ,", prev_pos)) != std::string::npos)
    {
        if (pos > prev_pos)
            v.push_back(str.substr(prev_pos, pos-prev_pos));
        prev_pos= pos+1;
    }
    if (prev_pos< str.length())
        v.push_back(str.substr(prev_pos, std::string::npos));


std::cout << v << std::endl;
std::cout << v[0] << std::endl;
std::cout << v[1] << std::endl;

}

Out put become:

[0.0, 2.1, 22.3]
0.0
2.1

Give it try

You can try this code in CodePad.

C++ code - 21 lines - codepad

Convert string vector to vector of double

Just using while loop. It will be safer.
Code is like below:

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

int main(){
std::string str = "[0.0, 2.1, 22.3]";

// copied from stackoverflow
std::vector<std::string> v ; //Use vector to add the words

    std::size_t prev_pos = 0, pos;
    while ((pos = str.find_first_of("[] ,", prev_pos)) != std::string::npos)
    {
        if (pos > prev_pos)
            v.push_back(str.substr(prev_pos, pos-prev_pos));
        prev_pos= pos+1;
    }
    if (prev_pos< str.length())
        v.push_back(str.substr(prev_pos, std::string::npos));


std::cout << v[1] << std::endl;
std::cout << v.size() << std::endl;

double* num;
num = new double[v.size()];

for(unsigned int i=0;i<v.size();i++){
    num[i] = std::stod(v[i]);
} 

std::cout << num[1] << std::endl;

}

try?

Codepad could not compile this due to compiler dependency or some error.
There is another page to test this code.
Online C++ Compiler - online editor

Written as a Function

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

void string2doublearray(std::string str, std::string delim, double *&num){
    
    std::vector<std::string> v ; //Use vector to add the words

    std::size_t prev_pos = 0, pos;
    while ((pos = str.find_first_of("[] ,", prev_pos)) != std::string::npos)
    {
        if (pos > prev_pos)
            v.push_back(str.substr(prev_pos, pos-prev_pos));
        prev_pos= pos+1;
    }
    if (prev_pos< str.length())
        v.push_back(str.substr(prev_pos, std::string::npos));

    num = new double[v.size()];

    for(unsigned int i=0;i<v.size();i++){
        num[i] = std::stof(v[i]);
    } 
    
    std::cout<< num[1] << std::endl;
}

int main(){
std::string str = "[0.0, 2.1, 22.3]";
std::string delim = "[] ,";
double *d;

string2doublearray(str,delim,d);

std::cout<< d[1] << std::endl;

}