Question About C++ Stoi Function

Hi everyone,

I was testing out the stoi built-in function in C++. However, the code gives me an error saying that "'stoi' was not declared in this scope". I am currently using C++ 17. I have put the code below. Can anyone tell me why my code does not work? Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    string num = "1001";
    cout << stoi(num, 0, 2) << endl;
    return 0;
}
It works just fine by clicking the "Edit & Run" to test it on http://cpp.sh/
Works OK with VS. What os/compiler are you using?
Another question would be, What output are you expecting?

And what do you get?

Andy
The output should be 9 - which is what I got with VS.
Also, I am using Code Blocks to run my code, and I have the C++ 17.12 version. However, the code does not work, so does anyone know why the code does not work?
You might have a recent code blocks / GCC installed, but you still have to do project->settings to tell it which version of C++ to adhere to.
See https://imgur.com/a/g6Ub9Gw

If you don't tell it what standard to follow, it follows the original.
1
2
3
4
5
6
$ g++ foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:10:27: error: ‘stoi’ was not declared in this scope
     cout << stoi(num, 0, 2) << endl;
                           ^
$ g++ -std=c++11 foo.cpp

If the Code Blocks 17.12 version does not correspond to C++ 17, can anyone tell me which Code Blocks version does correspond to C++ 17?
@codingfun

You are not understanding what salem c has said in his latest post.

With C++, a new standard these days is produced every 3 years. At the top of the cppreference page, one can see the various standards - C++98, C++03, C++11 etc:

https://en.cppreference.com/w/

Each new standard introduces new features for the language and library.

IDE's are not tied to one particular language, standard (or even compiler), although they may have a default one if none is specified. The fact that you have Code Blocks 17.12 version is mainly irrelevant. They could have called it Code Blocks 2020 if they wanted to. Instead, one can specify which compiler and standard to use in the project settings. For example I could choose clang++ or g++ as my compiler, and then specify any one of the C++ standards, provided that the version of the compiler is late enough. Some of the compilers will even do other languages: for instance, gcc means Gnu Compiler Collection, as well as C or C++, it can do FORTRAN, Go, Ada to name a few.

https://en.cppreference.com/w/cpp/symbol_index

With the link above, one can search for anything that is in the std namespace. It will tell the reader to which standard applies to that symbol ; or if one clicks on the link, the reference will show which standard applies to which parts of it. So that is why salem c had:

$ g++ -std=c++11 foo.cpp


std::stoi is part of C++11 standard.
In USACO, many contestants use C++ 17. Also, I know many of those contestants use Code Blocks. Which Code Blocks version do they use?
TheIdeasMan wrote:
They could have called it Code Blocks 2020 if they wanted to. Instead, one can specify which compiler and standard to use in the project settings.


The other obvious thing: if in doubt get the latest one - it's free.

Personally, I would get something else: If it doesn't do version control with git, and have background compilation (the squiggly lines if something is wrong), then I am not interested. VSCode seems to be better, or Visual Studio if one can afford it.
There's a free Community version of VS2019 https://visualstudio.microsoft.com/downloads/

seeplus wrote:
There's a free Community version of VS2019


Ah, I have VSCode, probably because I am on Linux - I wonder if it is the same as VS Community on Windows. Maybe a lesser version by the looks of the description on Wikipedia . I am happy with it so far.
VS Code is not VS2019. VS Code does not include any compilers etc. It's just an IDE. VS2019 is an IDE that also includes the compilers/debuggers etc etc. For c/c++ development, VS2019 only runs on Windows.
@seeplus

Thanks for your always excellent advice :+)
I am using Code Blocks to run my code, and I have the C++ 17.12 version

No, you have the version of the Code::Blocks IDE (Integrated Development Environment) that was released in December 2017. That has NOTHING to do whether the compiler used is capable of using C++17. What matters is the compiler the IDE uses.

The version numbers are the year.month the IDE was released.

A few things to note:

1. There is a newer IDE version of Code::Blocks, 20.03. Released March 2020.
https://www.codeblocks.org/downloads/binaries/

2. The workspace/project has to be configured to use C++17, I haven't found a way yet to make it the default version when creating a new C++ workspace/project.

3. The C::B IDE doesn't have to use the compiler bundled with the setup. It can be configured to use an alternate compiler. Even a different programming language, such as Fortran.

I found a newer version of MinGW-W64 that was released September 2020. I configured C::B to use that version so I have some rudimentary C++20 support if I want to use that standard. Normally I use C++17.

I installed this newer version of MinGW-W64 to a different HD location and created a new global compiler setting to use this new version. That new global compiler setting is my default.

Get the latest bundled C::B version, and then nab the later compiler. The bundled MinGW version can do C++17 since it was released in 2018. No C++20 if that matters.
Topic archived. No new replies allowed.