Hi everybody,

Please excuse the noob question as I am new to STL in C++. I am trying to write a very basic program which uses iota. However my compiler is complaining that iota does not exist in algorithm. Some other forums suggested including <backward/algo.h> as iota could have been depreciated. However my code still does not compile. Can some one take a look?

#include<iostream>
#include<vector>
#include<algorithm>
#include<iterator>
#include<backward/algo.h>

using namespace std;

int main(){
        vector<int> myVector;
        iota(myVector.begin(), myVector.end(),100);
        copy(myVector.begin(), myVector.end(), ostream_iterator<int>(cout," "));
        cout<<endl;
}

I am using g++ on Ubuntu and my compiler version is 4.4.3

Recommended Answers

All 3 Replies

itoa() is in <cstdlib> but is non-standard (meaning it is not implemented by all compilers).

This page shows an example of an alternative method using sprintf().

I'm pretty sure you could also use stringstream.

There are two versions of iota: an old non-standard version and the standard version added with C++11. I assume you're trying to use the latter, in which case you need to both have a version of GCC that supports that particular part of C++11 and turn on the -std=c++0x switch.

Unfortunately, I don't remember exactly which version of GCC started supporting iota, but it works just peachy in my version (4.6.0).

In the newest standard, this function should be found under the #include <numeric> header. Also, I'm pretty sure that GCC has had this function and header for a long time (they list it as part of their 1998/2003 standard library, although it shouldn't be, to be strict) so it should work, not so sure about MSVC though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.