Vlad
Vlad

Reputation: 13

How to convert numbers into vector of integers?

I want my program to ask the user to input any number and then store it into a std::vector where every single digit is allocated to a separate vector index:

input: 142

output vector: [1, 4, 2]

I tried this:

int main()
{
     std::vector<int> v;
     int number;
     cin >> number;
     for(unsigned int i = 100; i > 0; i/=10)
     {
         v.push_back(number/i);
         number -= (number/i)*i;
     }

     for(size_t i = 0; i < v.size(); ++i)
     {
         std::cout<<v[i]<<std::endl;
     }
}

It works. But what should I do when the input-length is unknown?

Upvotes: 1

Views: 2529

Answers (4)

JeJo
JeJo

Reputation: 32722

Use simply std::string and for each char(which is actually integers) of string convert to integer as follows: SEE LIVE HERE

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

int main()
{
    std::vector<int> v;
    std::string number = "123456789987654321";
    for(auto& Integer: number)
      v.emplace_back(static_cast<int>(Integer - '0'));

    for(const auto& it: v) std::cout << it << " ";
}

Output:

1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 

In case of entering unwanted characters/ user inputs(for example some negative numbers or even this: +-1234567), you can go for something with try-catch. try to convert the char to int, otherwise skip in the catch block as follows. SEE LIVE HERE

#include <iostream>
#include <vector>
#include <string>
#include <exception>
int main()
{
    std::vector<int> v;
    std::string number = "+-1234567";
    for(auto& Integer: number)
    {
        std::string Char(1, Integer); // convert to string
        try         {   v.emplace_back(std::stoi(Char));        }
        catch(...)  {   continue; }     // any case of exceptions

        /* or using much simpler std::isdigit from <cctype>
           by which conversion to std::string and try-catch can be avoided.

        if(std::isdigit(Integer))
            v.emplace_back(static_cast<int>(Integer - '0'));
        */
    }
    for(const auto& it: v) std::cout << it << " ";
}

Output:

1 2 3 4 5 6 7 

Edte: As per @Aconcagua suggested, included the solution with std::isdigit

Upvotes: 3

ccld44
ccld44

Reputation: 109

while(number) {
  v.push_back(number%10);
  number /= 10;
}
std::reverse(v.begin(), v.end());

Upvotes: 3

izlin
izlin

Reputation: 2138

I you want to stay with digits and std::deque is an option you could do the following:

int main()
{
    std::deque<int> v;
    int number;
    cin >> number;
    while(number != 0)
    {
      v.push_front(number%10);
      number = number/10;
    }
    for(size_t i = 0; i < v.size(); ++i)
    {
      std::cout<<v[i]<<std::endl;
    }
    return 0;
}

With the modulo operator you simply take the last digit and insert it in the front of the deque. Afterwards you "cut off" that digit with /10.

I used a deque, because you can't push front with vectors.

Upvotes: 1

I. Ahmed
I. Ahmed

Reputation: 2534

Change the for initilization unsigned int i = number rather than unsigned int i = 100. The re-written for statement will be:

for(unsigned int i = number; i > 0; i/=10)

Upvotes: 1

Related Questions