下面的代码是我做的一个简单测试,看看我是否可以通过转换进行编译。该代码编译并在visualstudio上正确输出此答案:
firsta
secondb
thirdc
但是使用 g++ main.cpp -o main 会给我这些错误:
main.cpp: In function 'int main()':
main.cpp:19:106: warning: lambda expressions only available with -std=c++0x or -std=gnu++0x [enabled by default]
main.cpp:19:107: error: no matching function for call to 'transform(std::vector<std::basic_string<char> >::iterator, std::vector<std::basic_string<char> >::iterator, std::vector<std::basic_string<char> >::iterator, std::vector<std::basic_string<char> >::iterator, main()::<lambda(const string&, const string&)>)'
main.cpp:19:107: note: candidates are:
/usr/include/c++/4.6/bits/stl_algo.h:4871:5: note: template<class _IIter, class _OIter, class _UnaryOperation> _OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation)
/usr/include/c++/4.6/bits/stl_algo.h:4907:5: note: template<class _IIter1, class _IIter2, class _OIter, class _BinaryOperation> _OIter std::transform(_IIter1, _IIter1, _IIter2, _OIter, _BinaryOperation)
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <iostream>
using namespace std;
int main()
{
vector<string> v, v2, v3;
v.push_back("first"); v.push_back("second"); v.push_back("third");
v2.push_back("a"); v2.push_back("b"); v2.push_back("c");
v3.resize(3);
transform(v.begin(), v.end(), v2.begin(), v3.begin(), [](const string &a, const string &b){return a + b;});
copy(v3.begin(), v3.end(), ostream_iterator<string>(cout, "\n"));
}