Re: Conversion of a number from string to vector<int>
"Anonymous" <anonymous@no.net> wrote in message
news:itkig0$ook$1@speranza.aioe.org...
Paul ha scritto:
He seems to be trying to convert a string to an int, this is what atoi
does.
What is incompetent about trying to provide a helpfull suggestion?
Basically, I am improving a constructor for big integers passed as strings
by the user. The class provides basic math operations in arbitrary
precision. It is everything done. When I implemented the constructor
initially, every char of the string was a digit in a vector<char>. This
was not really efficient. Factorial("1000") required about 70s on my
machine. So I decided to "group" more chars into ints, as (almost) many
chars as possible, that is by building a vector<int> from the given
number. This actually is 7 times faster than before, but is still not
perfect, since not all the possible bits of the integers are used. The
reason is that each digit in the vector<int> is in base B, where B is a
power of 10, not of two:
class BIGINT {
// Bitset must be signed (for diff. operation).
typedef signed int Bitset;
// vector is 25 times faster than list or twice than deque.
typedef std::vector<Bitset> Sequence;
// -1 to avoid overflows in sum.
static const int DGTS = std::numeric_limits<Bitset>::digits10 - 1;
BIGINT(const char* p = 0) {
// ...
size_t l = strlen(p);
for (size_t i = 0; i < l;) {
Bitset x = 0, f = 1;
for (int j = 0; j < DGTS && i < l; j++, i++, f *= 10)
x += (p[l - i - 1] - '0') * f;
module.push_back(x);
}
// ...
}
}
TBH I am still not 100% sure about your problem. I don't think this will
solve your porblem but is it the sort of thing you mean but using bigger
integers?
#include <iostream>
#include <vector>
#include <math.h>
std::vector<unsigned> numbers(std::string str, double r){
std::vector<unsigned> v;
std::string::iterator it;
unsigned int temp=0;
int power=str.length()-1;
double dec=10;
for (it=str.begin(); it<str.end(); it++, --power){
temp += (*it&15)*pow(dec,power);
}
while(temp){
v.push_back(temp%(int)r);
temp = temp/r;
}
return v;
}
int main(){
std::string str = "253";
double radix = 127;
std::vector<unsigned> v = numbers(str, radix);
for(int i=0; i< v.size(); i++){std::cout<< v[i]<<std::endl;}
}
But instead of taking a number like "253" you want to handle massive
integers which my temp variable wouldn't have the capacity for?