c++ - 将 vector 拆分为多个数组/vector C++

标签 c++ vector split type-conversion

我在将字符串 vector 拆分为更小的整数 vector\数组时遇到问题。我的输入 vector 数据如下所示:

std::vector<std::string> v(2);
v[0] = "0 14 150";
v[1] = "1 2 220";
//...

我知道一个解决方案,制作三个数组并使用 sstream 将数据转换为整数。但我想避免制作“意大利面条”代码。

谢谢, 彼得。

最佳答案

前段时间在stackoverflow上发现了一个split函数。不幸的是,我不能再发布链接了。

void split(const std::string & str, std::vector<std::string>& cont, const std::string & delims)
{
    std::size_t current, previous = 0;
    current = str.find_first_of(delims);
    while (current != std::string::npos) 
    {
        cont.push_back(std::move(str.substr(previous, current - previous)));
        previous = current + 1;
        current = str.find_first_of(delims, previous);
    }
    cont.push_back(std::move(str.substr(previous, current - previous)));
}

我需要在您的字符串中使用定界符(在您的情况下似乎是退格符)并在字符串 vector 的每个元素上调用该函数:

int main()
{
std::vector<std::string> vec{ "0 14 150","1 2 220" };
std::vector<std::vector<int>> intVec(3,std::vector<int>(vec.size()));
for (int i = 0; i < vec.size(); i++)
{
    std::vector<std::string> singleStr;
    split(vec[i], singleStr, " ");
    for (int j=0; j < singleStr.size();j++)
        intVec[j][i] = (std::stoi(singleStr[j]));
}

system("pause");
}

更通用的解决方案可能如下所示。您可以向 BasicVariant

添加更多类型
#include <string>
#include <vector>

class BasicVariant
{
private:
    std::string str;
public:
    BasicVariant(const std::string& _str) :str(_str) {}
    BasicVariant(int value) :str(std::to_string(value)) {}
    BasicVariant(double value) :str(std::to_string(value)) {}
    inline int toInt()const { return *this; }
    inline double toDouble()const { return *this; }
    inline std::string toString()const { return *this; }
    inline bool toBool()const { return toDouble(); }
    inline operator int()const { return std::stoi(str); }
    inline operator double()const { return std::stof(str); }
    inline operator std::string()const { return str; }
    inline operator bool()const { return toDouble(); }
};


template<typename T>
void split(const std::string& str, std::vector<T>& sink, const std::string& delims)
{
    std::size_t current, previous = 0;
    current = str.find_first_of(delims);
    while (current != std::string::npos)
    {
        sink.push_back(std::move(BasicVariant(str.substr(previous, current - previous))));
        previous = current + 1;
        current = str.find_first_of(delims, previous);
    }
    sink.push_back(std::move(BasicVariant(str.substr(previous, current - previous))));
}




int main()
{
    std::vector<std::string> vec{ "0 14 150","1 2 220" };
    std::vector<std::vector<int>> intVec(3, std::vector<int>(vec.size()));
    for (int i = 0; i < vec.size(); i++)
    {
        std::vector<int> row;
        split(vec[i], row, " ");
        for (int j = 0; j < row.size(); j++)
            intVec[j][i] = row[j];
    }

    system("pause");
}

关于c++ - 将 vector 拆分为多个数组/vector C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54424591/

相关文章:

c++ - std::vector 是否适合频繁调整大小?

css - 使用 css 更改 svg 图像的颜色

python - 根据分隔符拆分列表

java - 根据选定的字符数拆分 ArrayList 中的字符串

c++ - 是否可以将任何原始类型传递给需要指针的函数?

c++ - 快速复制 `std::vector<std::uint8_t>`

java - 如何检查点是否在对角线周围的矩形内?

java - 如何在 Java 中格式化文件中的文本?

c++ - C++ 中的 Java Collections.singleton() 是否有任何类似物?

c++ - 如何在 3D 场景中使用和设置轴