DevC++: How I program iterating over all elements from an STL collection properly? - dev-c++

I'm currently studying about loops and I found this found and I programmed it on DevC++ and here's my code.
#include < iostream >
#include < vector >
using namespace std;
int main()
{
std::vector < std::string > names = {"Albert Einstein", "Stephen Hawking", "Michael Ellis"}; for(std::vector< std::string >::iterator it = names.begin(); it != names.end(); ++it) {
std::cout << *it << std::endl;
}
}
after compiling it I had problem and here's what the compiler said:
C:\Users\chesc\Pictures\image\loops.cpp In function 'int main()':
9 89
C:\Users\chesc\Pictures\image\loops.cpp [Error] in C++98 'names' must be initialized by constructor, not by '{...}'
9 89 C:\Users\chesc\Pictures\image\loops.cpp [Error] could not convert '{"Albert Einstein", "Stephen Hawking", "Michael Ellis"}' from '' to 'std::vector >'

First, please format your question better. Put code into code sample.
To answer you. Your compiler is set to standard C++98. This standard does not allow the way of initialisation you used.
I suggest you to set your compiler for the newest standard it can support. It means C++11 and higher.
You can do it in Project Options -> Compiler -> Code Generation -> Language standards

Related

Unexpected error on declaring vector in c++

I'm getting an unexpectred error when I initialize a vector in the main.
I was expecting the following output:
0 1 2
I can't see why it's not working. I also writed the same code in another pc using the same compiler, and it works.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vett = {0,1,2};
for (int i : vett) {
cout << i << " ";
}
return 0;
}
error: could not convert '{0, 1, 2}' from '<brace-enclosed initializer list>' to 'std::vector<int>'|
You need to compile with at least C++11. List initialization came with C++11.
-std=c++11
You are compiling with something older than C++11, it does not support initializer list constructor.
If you are using Code::Blocks follow these steps:
Settings -> compiler -> compiler flags -> select C++11 or above

Cannot make unordered_map work

I have written fairly simple code to check out unordered_pair
// unordered_map example
#include <iostream>
#include <unordered_map>
int main (){
std::unordered_map<double, double> mymap = {
{5,2.3},
{7,34},
{4,12}
};
std::cout<<":::: unordered_map ::::"<<std::endl;
std::cout<<"5 ->"<<mymap[5]<<std::endl;
std::cout<<"7 ->"<<mymap[7]<<std::endl;
std::cout<<"4 ->"<<mymap[4]<<std::endl;
std::cout<<"Done ! ";
return 0;
}
I get the following error
unordered_map.cpp:5:37: error: non-aggregate type 'std::unordered_map<double, double>' cannot be initialized with an initializer list
std::unordered_map<double, double> mymap = {
^ ~
1 error generated.
Compilation failed
I am working on OSX and using geany.
Build command in Geany is :
g++ -Wall -c "%f"
unordered_map was introduced in the C++11 standard. If your g++ version supports it, you can enable c++11 standard with option -std=c++11
is there a way to use associative containers in older standards
std::map is an associative container. It exists in all c++ standards.
I was trying to use unordered_map because it is faster than map
unordered_map is faster in some cases. map is faster in other cases. Don't assume one way until you have measured.
However, using map without -std=c++11 still gives me same error as above
That's because you use list initialization which also requires c++11.
The old way of initializing a map:
std::map<int, int> m;
m[1] = 3;
m[2] = 2;
m[3] = 1;
Boost has a neat template for similar syntax to list initialization:
map<int,int> next = map_list_of(1,2)(2,3)(3,4)(4,5)(5,6);

Stoi was not declared in scope - Code::blocks

Edit: I'm trying to tell it to work with C++11 by clicking "Have g++ follow the C++11 ISO C++ language standard" in the compiler flags.
I'm getting stoi was not declared in scope, and I've added c++11 to Code::Blocks; I've added compatibility in Settings -> Compilers -> Compiler flags, but it still keeps giving me that error.
And when I try to do atoi or strtol I get the following error:
C:\Users\user\Desktop\Programming\NewProject\main.cpp|19|error: cannot
convert 'std::string {aka std::basic_string}' to 'const char*'
for argument '1' to 'long int strtol(const char*, char**, int)'|
My code:
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
string numberGuessed;
int numberGuessedint = 0;
do {
cout << "Guess a number between 1 and 10: ";
getline(cin, numberGuessed);
numberGuessedint = stoi(numberGuessed);
cout << numberGuessedint << endl;
} while(numberGuessedint != 4);
cout << "You win!" << endl;
return 0;
}
It is a known bug in MinGW bundled with Code::Blocks.
You can apply a patch: http://tehsausage.com/mingw-to-string
Or download fresh version of MinGW (preferable with threading support, as you lack it too) and replace one you have right now.
To use atoi you need:
numberGuessedint = atoi(numberGuessed.c_str());
I am writing a solution which worked for me. As I found in most of the solutions posted on stack overflow, code blocks earlier versions contain a bug. So I deleted my older code blocks version and installed a new version 17.12 from code blocks website.
Then I just clicked on "Have g++ follow the C++11 ISO C++ language standard" in the compiler flags.
Settings -> Compilers -> Compiler flags.
It works for me(I am using windows 7).

Using auto in loop error "auto must have an initializer"

I want to use the auto keyword but get this error when compiling (Visual C++ Express 2010)
typedef std::list<int> MyList;
int main()
{
const int args[] = {0, 1, 2};
MyList myList(std::begin(args), std::end(args));
for(auto& value : myList)
{
std::cout << value << std::endl;
}
}
Output:
error C2143: syntax error : missing ',' before ':'
error C2530: 'value' : references must be initialized
error C3531: 'value': a symbol whose type contains 'auto' must have an initializer
The C++ compiler in VS2010 does support range-based for loops, but with a pre-standard syntax.
(And seriously, you can't expect a compiler finished about 2 years before the Standard to be fully compliant)
for each (int& value in myList)
{
std::cout << value << std::endl;
}
There are two issues in your code:
You need to explicitly include the headers for the functionality you use.
#include <list>
#include <iostream>
VS2010 does not support C++11 range-based "for" loops. The feature was implemented much later. See the following table: http://msdn.microsoft.com/en-us/library/hh567368.aspx

Range For loop help: Expected initializer before ":" token

I am completely new to C++ and I am now following the C++ Primer book.
I wrote a small example about strings, here is the code:
#include <iostream>
#include <string>
#include <cctype>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main() {
string s("Hello World");
for (auto &c : s)
c = toupper(c);
cout << s << endl;
return 0;
}
I am on Linux with GCC version 4.4.6 and I tried to compile this code with:
g++ test_strings.c -std=c++0x
but got the following errors:
test_strings.c: In function 'int main()':
test_strings.c:14: error: expected initializer before ':' token
test_strings.c:19: error: expected primary-expression before 'return'
test_strings.c:19: error: expected ')' before 'return'
I copied the program from the textbook, so I though it was a misspelling but after a check and trying searching on the web and updating my gcc the error reminds. Help will be greatly appreciated, thanks in advance.
As per the C++0x/C++11 Support in GCC page, you need to be running gcc 4.6 to get the range-for feature.
The 4.6 changes page contains:
Improved experimental support for the upcoming C++0x ISO C++ standard, including support for constexpr (thanks to Gabriel Dos Reis and Jason Merrill), nullptr (thanks to Magnus Fromreide), noexcept, unrestricted unions, range-based for loops (thanks to Rodrigo Rivas Costa), opaque enum declarations (thanks also to Rodrigo), implicitly deleted functions and implicit move constructors.
Since you're running gcc 4.4.6, it's not available to you.