stoi, stoull not supported by code::blocks?

I'm running Code::Blocks with the latest TDM-GCC for C++11 support and I can't run functions like stoi (string to integer) and stoull which are well documented on this website... I can run parts of the <thread> library that I tried, but no stoi?? Any advice?? :-)

AeonFlux1212
Are you prefixing their names with std:: like you should? Also, why do you want to use these functions if at the same time you want to use C++11? There's "more modern" ways to do this ;)
Last edited on
Yeah I'm prefixing them with std::, but it seems there's just no support for it... but I found these functions looking in the string library, because I wanted to work with strings, what other modern options are there to extract numbers from strings? because basically that's what I need to do... I don't really need c++11...
Last edited on
The two ways I recommend are:

1. Boost lexical_cast:
1
2
std::string str = "1234567890";
auto num = boost::lexical_cast<int>(str);
http://www.boost.org/doc/libs/1_55_0/doc/html/boost_lexical_cast.html

2. Plain C++ std::istringstream:
1
2
3
std::string str = "1234567890";
int num;
std::istringstream{str} >> num;
http://www.cplusplus.com/reference/sstream/istringstream/
http://en.cppreference.com/w/cpp/io/basic_istringstream
@LB std::stoi is the C++11 way of doing it http://www.cplusplus.com/reference/string/stoi/

@AeonFlux1212
Have you added -std=c++11 to compiler options?
If that's not the issue you may need to patch mingw http://stackoverflow.com/a/16132279
Last edited on
look at that...

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
35
36
long long stringToLongLong(string myString, std::string::iterator pointerToWhereYouLeftItLastTime = NULL, unsigned short base = 10)
{
    std::string::iterator beginningOfNumber;
    
    long long returnValue = 0;
    
    if(pointerToWhereYouLeftItLastTime == NULL)
    {
        pointerToWhereYouLeftItLastTime = myString.begin();
    }
    
    while(*pointerToWhereYouLeftItLastTime < 48 || *pointerToWhereYouLeftItLastTime > 57)
    {
        pointerToWhereYouLeftItLastTime++;
    }
    
    if(*pointerToWhereYouLeftItLastTime >= 48 && *pointerToWhereYouLeftItLastTime <= 57)
    {
        beginningOfNumber = pointerToWhereYouLeftItLastTime;
    }
    while(*pointerToWhereYouLeftItLastTime >= 48 && *pointerToWhereYouLeftItLastTime <= 57)
    {
        pointerToWhereYouLeftItLastTime++;
    }
    
    lenght = pointerToWhereYouLeftItLastTime - beginningOfNumber;
    
    for(unsigned long long lenght = pointerToWhereYouLeftItLastTime - beginningOfNumber; lenght > 0; lenght--)
    {
        returnValue += (*beginningOfNumber)*(base^(lenght-1));
        
        beginningOfNumber++;
    }
    
    return returnValue;
}


still have to implement different bases like 2, 8, and 16... and also I have to tell it to stop when it reaches the end of the string
Last edited on
@naraku: the naming convention threw me off, I always think it is a C function.
here's the complete version, custom stoll() hehe...

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
long long stringToLongLong(string myString, std::string::iterator pointerToWhereYouLeftItLastTime = NULL, unsigned short base = 10)
{
    std::string::iterator beginningOfNumber = NULL;
    
    long long returnValue = 0;
    
    if(pointerToWhereYouLeftItLastTime == NULL)
    {
        pointerToWhereYouLeftItLastTime = myString.begin();
    }
    
    if(base >= 2 || base <= 10)
    {
        while(pointerToWhereYouLeftItLastTime != myString.end() && (*pointerToWhereYouLeftItLastTime < 48 || *pointerToWhereYouLeftItLastTime > 48 + base - 1))
        {
            pointerToWhereYouLeftItLastTime++;
        }
        
        if(pointerToWhereYouLeftItLastTime != myString.end())
        {
            beginningOfNumber = pointerToWhereYouLeftItLastTime;
        }
        while(pointerToWhereYouLeftItLastTime != myString.end() && *pointerToWhereYouLeftItLastTime >= 48 && *pointerToWhereYouLeftItLastTime <= 48 + base - 1)
        {
            pointerToWhereYouLeftItLastTime++;
        }
    }
    else if(base <= 16)
    {
        while(pointerToWhereYouLeftItLastTime != myString.end() && (*pointerToWhereYouLeftItLastTime < 48 || *pointerToWhereYouLeftItLastTime > 57) && (*pointerToWhereYouLeftItLastTime < 65 || *pointerToWhereYouLeftItLastTime > 65 + base - 11) && (*pointerToWhereYouLeftItLastTime < 97 || *pointerToWhereYouLeftItLastTime > 97 + base - 11))
        {
            pointerToWhereYouLeftItLastTime++;
        }
        
        if(pointerToWhereYouLeftItLastTime != myString.end())
        {
            beginningOfNumber = pointerToWhereYouLeftItLastTime;
        }
        while(pointerToWhereYouLeftItLastTime != myString.end() && (*pointerToWhereYouLeftItLastTime >= 48 && *pointerToWhereYouLeftItLastTime <= 57 || *pointerToWhereYouLeftItLastTime >= 65 && *pointerToWhereYouLeftItLastTime <= 65 + base - 11 || *pointerToWhereYouLeftItLastTime >= 97 && *pointerToWhereYouLeftItLastTime <= 97 + base - 11))
        {
            pointerToWhereYouLeftItLastTime++;
        }
    }
    
    if(beginningOfNumber)
    {
        for(unsigned long long lenght = pointerToWhereYouLeftItLastTime - beginningOfNumber; lenght > 0; lenght--)
        {
            returnValue += (*beginningOfNumber)*(base^(lenght-1));
            
            beginningOfNumber++;
        }
    }
    
    return returnValue;
}
Last edited on
Was is really necessary to reinvent the wheel?

Off-topic: first time I remembered how to spell 'necessary' correctly from memory! Small victory :)
@L B and @naraku9333 thanks for the input guys... I don't know much about streams, so I'll check it out...

Cheers,
AeonFlux1212
@L B hehe yeah well at least this one compiles and I'll be able to move on lol
AeonFlux1212 wrote:
@L B hehe yeah well at least this one compiles and I'll be able to move on lol
What compiler are you using? g++ (ideone) and VC++ don't compile. http://ideone.com/Z834xz
I just noticed, your implementation is not correct - you are using bitwise XOR instead of std::pow
Also, why the magic numbers? This code doesn't even look remotely correct.
Last edited on
This slightly modified version works http://ideone.com/dw8YXU

The main problems are the use of xor, as LB said, but also you are using iterators like they are pointers, but they aren't.
Last edited on
ok... I have a big problem... it doesn't compile... I get an error message concerning my second parameter:

error: could not convert '0' from 'int' to 'std::basic_string<char>::iterator {aka __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >}'|

error: conversion from 'int' to non-scalar type 'std::basic_string<char>::iterator {aka __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >}' requested|
C:\Program Files\CodeBlocks\projets\cpp.com7\main.cpp||In function 'long long int stringToLongLong(std::string, std::basic_string<char>::iterator, short unsigned int)':|

error: conversion from 'int' to non-scalar type 


and it goes on and on and on like this... I thought this string iterator would work like a list or vector iterator for example... maybe I can't assign it a NULL value or something.... std::pow() -> noted, didn't use it because it involved only floats and doubles but I'll fix it... the "Magic Numbers" are actually the decimal values for numerical chars (0 to 9 are char 48 to 57), and since I wanted to add functionnality to up to base 16, I added chars 'a' to 'f', both lower and uppercase... I always work with chars this way instead of comparing with they're actual symbol, I don't think it should be a problem... but I really don't know what's wrong with this string iterator... I'll have to read some more I guess :-) or if you have any ideas please share them!

Thanks,
AeonFlux1212
I could also add, I forgot to call it by reference too (the second parameter... or maybe it should be a pointer???) but even with that it doesn't compile... well the purpose of it is to be able to read multiple numbers in a large string containing many letters and random caracters, always starting the read operation where you left it after the last call to it, so you can do that until you've reached the end of the string and have extracted all the valuable numbers inside of it, wheter there's more than one or not. The third parameter is the base of the numbers you want to extract, is it binary, octal, decimal, hexadecimal, etc... defaulted to 10 (decimal) for convenience...
hmm yeah I thought this iterator was a pointer, I'll have to check it out
here's a simplified version, got tired of all those pointers and iterators... I still have the problem that it returns 0 when no number is found... when it should return something else, like some sort of NO_NUMBER constant, but that's secondary for now... thanks again for the help guys... will definitly look into this later about the iterators and pointers...

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
long long stringToLongLong(string myString, unsigned long long& index, unsigned short base)
{
    long long beginningOfNumber = -1;

    long long returnValue = 0;

    if(base >= 2 || base <= 10)
    {
        while(index < myString.size() && (myString[index] < 48 || myString[index] > 48 + base - 1))
        {
            index++;
        }

        if(index < myString.size())
        {
            beginningOfNumber = index;
        }
        while(index < myString.size() && myString[index] >= 48 && myString[index] <= 48 + base - 1)
        {
            index++;
        }
    }
    else if(base <= 16)
    {
        while(index < myString.size() && (myString[index] < 48 || myString[index] > 57) && (myString[index] < 65 || myString[index] > 65 + base - 11) && (myString[index] < 97 || myString[index] > 97 + base - 11))
        {
            index++;
        }

        if(index < myString.size())
        {
            beginningOfNumber = index;
        }
        while(index < myString.size() && (myString[index] >= 48 && myString[index] <= 57 || myString[index] >= 65 && myString[index] <= 65 + base - 11 || myString[index] >= 97 && myString[index] <= 97 + base - 11))
        {
            index++;
        }
    }

    if(beginningOfNumber != -1)
    {
        for(unsigned long long lenght = index - beginningOfNumber; lenght > 0; lenght--)
        {
            if(myString[beginningOfNumber] == 48)
            {
                returnValue += 0*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 49)
            {
                returnValue += 1*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 50)
            {
                returnValue += 2*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 51)
            {
                returnValue += 3*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 52)
            {
                returnValue += 4*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 53)
            {
                returnValue += 5*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 54)
            {
                returnValue += 6*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 55)
            {
                returnValue += 7*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 56)
            {
                returnValue += 8*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 57)
            {
                returnValue += 9*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 65 || myString[beginningOfNumber] == 97)
            {
                returnValue += 10*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 66 || myString[beginningOfNumber] == 98)
            {
                returnValue += 11*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 67 || myString[beginningOfNumber] == 99)
            {
                returnValue += 12*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 68 || myString[beginningOfNumber] == 100)
            {
                returnValue += 13*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 69 || myString[beginningOfNumber] == 101)
            {
                returnValue += 14*pow(base, lenght-1);
            }
            else if(myString[beginningOfNumber] == 70 || myString[beginningOfNumber] == 102)
            {
                returnValue += 15*pow(base, lenght-1);
            }

            beginningOfNumber++;
        }
    }

    return returnValue;
}
You are putting yourself through purgatory despite receiving the acceptance letter to heaven. Just use one of my two suggestions from my first reply to this topic :p
Topic archived. No new replies allowed.