Problem about converting char arrays to int and then store into list<int>

Hello everybody!

I have a major problem about some lists.
That's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
#include<list>
#include<cstring>
#include<cstdlib>
using namespace std;
list<int> BIG_NUMBERS_SUM(char x[], char y[]);
int main()
{
    char x[1000000], y[1000000];

    cout<<"First number: ";
    cin.getline(x, 1000000);
    cout<<"The second number: ";
    cin.getline(y, 1000000);

    BIG_NUMBERS_SUM(x,y);

    return 0;
}

list<int> BIG_NUMBERS_SUM(char x[], char y[])    // functia care face toata treaba :))
{
    list<int> number_1;                    // declararea listelor
    list<int> number_2;
    list<int> sum;

    list<int>::iterator i1;                // declararea iteratorilor
    list<int>::iterator i2;

    // how can I convert the char arrays to int and then to store the values in those lists?

    for(i1 = number_1.begin(); i1!=number_1.end(); i1++)
     cout<<*i1<<" ";
}


I want to create a program that add two big numbers (with 1 milion digits).
Well, I want to to that using lists from STL. I want to read the numbers as char arrays and then to convert the char - arrays into integers, and then to store them into those two lists.

I know that atoi() function may help me but I don't really manage how to put every digit of that number in a location of a list. atoi() doesn't work if I want to convert only a character (for instance atoi(x[i]), it works only for the entire string.

Also I tried with assign(atoi(x)) method but it must have two parameters and it doesn't help me very much.

Please excuse my english, I am in a hurry and I hope that one of you might help me.

Thank you respectfully.
1
2
3
4
5
6
7
8
9
char a[] = "123456789";
std::list<int> number_1;

std::transform( a, a + strlen( a ), std::back_inserter( number_1 ),
		[]( char c ) { return ( c - '0' ); } );

std::cout << std::accumulate( number_1.begin(), number_1.end(), 0, 
                              []( int s, int x ) { return ( 10 * s + x ); } )
          << std::endl;
Last edited on
Wow!

Can you explain a bit what does that code? I try to understand those functions: transform and accumulate but I don't manage.

Thank you respectfully.
The first standard algorithm does the following

1
2
3
4
for ( const char *p = a; *p; ++p )
{
   number_1.push_back( *p - '0' );
}


The second standard algorithm generates the number from elements of the list

1
2
3
4
5
6
7
8
int n = 0;

for ( std::list<int>::iterator it = number_1.begin(); it != number_1.end(); ++it )
{
   n = 10 * n + *it;
}

std::cout << n << std::endl;
Last edited on
It demonstrates that in C++ any task can be done in different ways.:)
C++11 lambda functions + algorithms!
http://en.cppreference.com/w/cpp/algorithm/transform
http://en.cppreference.com/w/cpp/algorithm/accumulate
http://en.cppreference.com/w/cpp/language/lambda

@ Cosmin: if Vlad's first examples don't compile for you, use the "translations" that he gave.
(Otherwise, get a newer compiler that has good support for C++11.)
Allright guys, thank you very much!
C++11 lambda functions + algorithms!

Tnx CatFish2 !
Topic archived. No new replies allowed.