将整数字符串转换为整数的2D向量

问题描述 投票:1回答:3

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

1234
2345
3456
4567

给定相同的数字平方,但每一个都是一个字符串,我怎样才能实现一个2D矢量,它将采用正方形的每个字符,然后首先将每个字符转换为int,然后存储到行和列的2D矢量中制作完全相同的方块?

我知道2D矢量需要

vector<vector<int>> square_vector;

但是我无法获取所有成对的行和列。

编辑:如果我的方格是

1234
2345
3456
4567

我想首先通过第一行1234。然后在那一行中,我想通过每个列字符1, 2, 3, 4并将每个字符转换为int。转换后,我想将push_back作为一行放入2D矢量。一行完成后,我想继续下一行并执行相同的任务。

c++ algorithm c++11 stdvector
3个回答
3
投票

但是我无法获取所有成对的行和列。

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

#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 

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;
}

虽然你应该自己尝试一下,但我已经在这里为你做了。


1
投票

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

这有些含糊不清。接受的答案将广场视为

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.