1

Given my desired output to be a square output (don't need any spaces between them):

1234
2345
3456
4567

Given the same square of numbers, but each one of them being a std::string, how can I implement a 2D vector that will take each character of the square and first convert each character into an int and then store into a 2D vector of rows and column to make the exact same square?

I know that a 2D vector needs to be

vector<vector<int>> square_vector;

But I was having trouble taking all of the paired rows and column.

EDIT: If my square is

1234
2345
3456
4567

I want to go through the first row 1234 first. Then within that row, I want to go through each column character 1, 2, 3, 4 and convert each character to an int. After converting, I want to push_back as a row into the 2D vector. After a row is complete, I would like to move on to the next row and perform the same task.

JeJo
  • 30,635
  • 6
  • 49
  • 88
Dracep
  • 388
  • 2
  • 13
  • 6
    Can you give a [mcve] of your attempt? – George Oct 15 '18 at 12:20
  • @George okay I have made an edit and tried my best to be more detailed in what I would like to do. Please let me know if it is still not clear enough. – Dracep Oct 15 '18 at 12:31
  • 1
    it is clear what you want to do. what is not clear: what is the problem? Please show your code and explain where/why you got stuck (maybe also read (again?) about [mcve]) – 463035818_is_not_an_ai Oct 15 '18 at 12:35
  • 1
    What did you try to achieve your solution? – Thomas Sablik Oct 15 '18 at 12:35
  • @user463035818 thanks for the heads up, in the future I will also post my attempted code. This time I was working with only a 2D vector and was not aware that a 1D vector would also be needed to create a square. – Dracep Oct 15 '18 at 12:49

3 Answers3

3

But I was having trouble taking all of the paired rows and column.

As you are using std::vector, why don't you simply use the range based for loop to do the job. Hope the comments will help you to go through the code.

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

output:

1 2 3 4 
2 3 4 5 
3 4 5 6 
4 5 6 7 
JeJo
  • 30,635
  • 6
  • 49
  • 88
  • 1
    Thank you for your solution. I was also wondering how to print each item in the vector and your for loop printing each item made perfect sense. – Dracep Oct 15 '18 at 12:59
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;
}

Although you should have tried it yourself, but I have done it for you here.

hellow
  • 12,430
  • 7
  • 56
  • 79
anupam691997
  • 310
  • 2
  • 8
  • 1
    Thank you, my issue was that I was not cycling through a 1D vector in my 2D vector. I will post my code and show what I have had in my future posts. – Dracep Oct 15 '18 at 12:45
  • 2
    https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h – Evg Oct 15 '18 at 13:00
1

Given the same square of numbers, but each one of them being a string

There's some ambiguity in that. The accepted answer sees the square as

std::vector<std::string>

but it could also be interpreted as

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

This version handles both:

#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);
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108