【问题标题】:How to convert the string of integer to 2D vector of integers?如何将整数字符串转换为整数的二维向量?
【发布时间】:2018-10-15 12:19:52
【问题描述】:

鉴于我想要的输出是方形输出(它们之间不需要任何空格):

1234
2345
3456
4567

给定相同的数字正方形,但每个数字都是 std::string,我如何实现一个 2D 向量,它将获取正方形的每个字符并首先将每个字符转换为int,然后存储到行和列的二维向量中以制作完全相同的正方形?

我知道需要一个二维向量

vector<vector<int>> square_vector;

但我无法处理所有配对的行和列。

编辑: 如果我的方格是

1234
2345
3456
4567

我想先通过第一行1234。然后在该行中,我想遍历每个列字符1, 2, 3, 4 并将每个字符转换为int。转换后,我想将push_back 作为一行插入到二维向量中。一行完成后,我想继续下一行并执行相同的任务。

【问题讨论】:

  • 你可以给minimal reproducible example你的尝试吗?
  • @George 好的,我已经进行了编辑,并尽我所能更详细地说明我想做的事情。如果还不够清楚,请告诉我。
  • 很清楚你想做什么。什么不清楚:有什么问题?请展示您的代码并解释您卡在哪里/为什么卡住(也许还阅读(再次?)关于minimal reproducible example
  • 您尝试过什么解决方案?
  • @user463035818 感谢您的提醒,以后我也会发布我尝试过的代码。这次我只使用 2D 矢量,不知道创建正方形还需要 1D 矢量。

标签: c++ algorithm c++11 stdvector stdstring


【解决方案1】:

但我无法处理所有配对的行和列。

当您使用std::vector 时,为什么不简单地使用range based for loop 来完成这项工作。希望 cmets 能帮助您完成代码。

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

int main()
{
    // vector of strings to be converted
    std::vector<std::string> strVec{ "1234", "2345", "3456", "4567" };
    // get the squre size
    const std::size_t size = strVec[0].size();
    // resulting 2D vector
    std::vector<std::vector<int>> result;   result.reserve(size);

    for (const std::string& strInteger : strVec)
    {
        std::vector<int> rawVec; rawVec.reserve(size);
        for (const char Char : strInteger)
            // if (std::isdigit(Char))     //(optional check)
            rawVec.emplace_back(static_cast<int>(Char - '0'));
        // save the row to the 2D vector
        result.emplace_back(rawVec);
    }
    // print them
    for (const std::vector<int>& eachRaw : result)
    {
        for (const int Integer : eachRaw)
            std::cout << Integer << " ";
        std::cout << std::endl;
    }
}

输出

1 2 3 4 
2 3 4 5 
3 4 5 6 
4 5 6 7 

【讨论】:

  • 感谢您的解决方案。我还想知道如何打印向量中的每个项目,而你的 for 循环打印每个项目非常有意义。
【解决方案2】:
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<vector<int>> square_vector;
    int n,a;
    // taking input the number of strings
    cin>>n;

    char s[100];
    vector<int> i;

    while(n--)
    {
        // take input string one by one
        cin>>s;
        a=0;

        //push character by character of that string into vector
        while(s[a]!='\0')
        {
            i.push_back(s[a]-'0');
            a++;
        }

        // push that vector into 2D vector
        square_vector.push_back(i);
        i.clear();
    }

    int j;

    //printing your 2D vector
    for(a=0;a<square_vector.size();a++)
    {
        i=square_vector.at(a);

        for(j=0;j<i.size();j++)
            cout<<i.at(j);
        cout<<"\n"; 
    }

    return 0;
}

虽然你应该自己尝试过,但我在这里为你做了。

【讨论】:

【解决方案3】:

给定相同的数字平方,但每个数字都是一个字符串

这有一些模棱两可的地方。接受的答案将正方形视为

std::vector<std::string>

但也可以理解为

std::vector<std::vector<std::string>>

这个版本同时处理:

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

template<typename T>
int toint(const T& x) {
    return std::stoi(x);
}
template<>
int toint(const char& x) {
    return x-'0';
}

template<class T>
std::vector<int> line2intvec(const T& in) {
    std::vector<int> result;
    for(auto& e : in) {
        result.push_back( toint(e) );
    }
    return result;
}

void print_vvi(const std::vector<std::vector<int>>& vvi) {
    for(auto& l : vvi) {
        for(auto& r : l) {
            std::cout << " " << r;
        }
        std::cout << "\n";
    }
}

int main() {
    std::vector<std::string> vs = {
        "1234",
        "2345",
        "3456",
        "4567"
    };
    std::vector<std::vector<std::string>> vvs = {
        { "1", "2", "3", "4" },
        { "2", "3", "4", "5" },
        { "3", "4", "5", "6" },
        { "4", "5", "6", "7" }
    };

    std::vector<std::vector<int>> result1;
    std::vector<std::vector<int>> result2;

    for(auto& s : vs) {
        result1.emplace_back( line2intvec(s) );
    }
    print_vvi(result1);

    for(auto& v : vvs) {
        result2.emplace_back( line2intvec(v) );
    }
    print_vvi(result2);
}

【讨论】:

    猜你喜欢
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 2016-07-09
    相关资源
    最近更新 更多