Why prev of iterator does work on via mac gcc

I tried to use prev for map after checking out
http://www.cplusplus.com/reference/iterator/prev/

But there's one error "test2.cpp:13: error: ‘prev’ was not declared in this scope
"

Here's my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <set>
#include <map>
#include <typeinfo>
#include <iterator>
#include <algorithm> 
using namespace std;

int main() {
    map<int, int> testMap;
    testMap.insert(make_pair(1,1));
    testMap.insert(make_pair(2,1));
    for (map<int,int>::iterator it = testMap.begin(); it != prev(testMap.end()); it++) {
        cout<<"("<<it->first<<","<<it->second<<")"<<"\t";
    }
    
}


and gcc version:
gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)

Could someone help me out why's that, looks like prev is not defined in this gcc version here?
That's an old version of GCC (current version is 4.8.2).

std::prev() is a feature of C++11 (the C++ standard of 2011).
Your compiler, being too old, does not implement it.

I realize this is an un-solution, but if you don't need to use std::prev() then don't use it.
Your for() loop leaves out the last element in the map, is this really what you want?

Otherwise upgrade your compiler to a more recent version.
Thank you Catfish for the quick reply.
Yes, this is what I want, actually I just want to have an example for "prev".

Btw, could you furthermore point out how can we know it's "a feature of C++11" and which gcc version has it?
Features of C++11 are marked in the documentation.
See again the page for std::prev() and look for the C++11 Warning icon.
(Little C++11 icons appear in the menu on the left as well, where needed.)

http://www.cplusplus.com/reference/iterator/prev/

An alternative Wiki-style C++ reference that also marks C++11 features is:
http://en.cppreference.com/w/

As for C++11 support in GNU GCC:
http://gcc.gnu.org/projects/cxx0x.html

If you're using MacOS, perhaps you should look into Clang:
http://clang.llvm.org/cxx_status.html
Last edited on
Thank you Catfish, so helpful.
Today I tried with latest gcc but still no luck...

test3.cpp: In function ‘int main()’:
test3.cpp:13:79: error: ‘prev’ was not declared in this scope
for (map<int,int>::iterator it = testMap.begin(); it != prev(testMap.end()); it++) {
^
risestoke-lm:CppCheney qili$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-apple-darwin12.4.0/4.9.0/lto-wrapper
Target: x86_64-apple-darwin12.4.0
Configured with: ../gcc-4.9-20130811/configure --enable-languages=fortran,c++
Thread model: posix
gcc version 4.9.0 20130811 (experimental) (GCC)

More idea?
As far as I know, the default standard of GCC is usually C++98.
You can enforce a standard by using the -std command line flag.

$ g++ -std=c++98 test3.cpp -o test3_cpp98
$ g++ -std=c++11 test3.cpp -o test3_cpp11


And since we're here, it's good practice to extend warnings:

$ g++ -Wall -Wextra -Wpedantic -std=c++11 test3.cpp -o test3

Thank you Catfish,
It's insightful, from "man gcc", confirmed what you mentioned.
1
2
           gnu++03
               GNU dialect of -std=c++98.  This is the default for C++ code.
Topic archived. No new replies allowed.