简体   繁体   English

从函数填充的字符串的推导向量中的字符串到int

[英]String to int from deduced vector of strings filled by a function

I have a string 111;222;333 which I want to convert to three integers. 我有一个字符串111;222;333 ,我想将其转换为三个整数。

First I split the string 首先,我拆分了字符串

std::vector<std::string> split(...){ ... };

The return values are stored in vector of deduced type 返回值存储在推导类型的vector

std::vector splitVals {split(...)};

If I then want to convert the values to integer like so 如果我然后想要将值转换为整数,就像这样

int foo1 {std::stoi(splitVals[0])};

The stoi function is complaining, because the deduced type of the vector is std::vector<std::vector<std::string>, std::allocator<std::vector<std::string>>> , but if I don't let the type to be deduced, everything works as intended. stoi函数是抱怨的,因为推导出的向量类型是std::vector<std::vector<std::string>, std::allocator<std::vector<std::string>>> ,但是如果我不会让类型被推断,一切都按预期工作。

std::vector<std::string> splitVals {split(...)};
int foo1 {std::stoi(splitVals[0])};

std::stoi now can work because the input value is std::string . std::stoi现在可以工作,因为输入值是std::string The issue seems to start with the vector being initialized from a function which returns the std::vector<std::string> . 问题似乎始于从返回std::vector<std::string>的函数初始化std::vector<std::string> Is there a way to benefit from C++17 class template argument deduction, without these limitations? 有没有办法从C ++ 17类模板参数推导中受益,没有这些限制?

Don't use brace initialization. 不要使用大括号初始化。 It favors the std::initializer_list constructor, and so the vector is deduced as holding sub-vectors for elements. 它支持std::initializer_list构造函数,因此向量推导为保持元素的子向量。 Using parentheses should clear it right up. 使用括号应该清除它。

std::vector splitVals (split(...));

Now the copy deduction guide is favored and the type held by the vector should be deduced as std::string . 现在复制推导指南是受青睐的,并且矢量所保持的类型应该推导为std::string

Wow. 哇。 8 upvotes and accepted answer. 8赞成并接受了答案。 So, basically nothing more to add . 所以,基本上没什么可补充的。 . .

I just want to come back to the underlying mentioned task: 我只想回到下面提到的任务:

I have a string 111;222;333 which I want to convert to three integers. 我有一个字符串111; 222; 333,我想将其转换为三个整数。

This a typical example for parsing for a CSV. 这是解析CSV的典型示例。 And this can be easily done with a one-liner (one statement). 这可以通过单行(一个声明)轻松完成。 Using std::transform and sregex_toke_iterator . 使用std::transformsregex_toke_iterator

For completeness, I will show the code for that. 为了完整起见,我将展示代码。

#include <iostream>
#include <vector>
#include <string>
#include <regex>
#include <iterator>
#include <algorithm>

int main()
{
    // The test data
    std::string data("111;222;333;444");

    // Here we will see the result
    std::vector<int> values{};

    // This we will look up in the string
    std::regex re("(\\d+)");

    // Put all tokens into vector
    std::transform(
        std::sregex_token_iterator(data.begin(), data.end(), re, 1), 
        std::sregex_token_iterator(),
        std::back_inserter(values),
        [](const std::string& s){ return std::stoi(s); }
    );

    // Show debug output    
    std::copy(values.begin(),values.end(),std::ostream_iterator<int>(std::cout,"\n"));

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM