Этот код (не "работает" правильно), но компилируется в VS 2010, но не собирается на GCC 4.5.1
#include <iostream>
#include <vector>//not necessary second > should skip like brackets
using namespace std;
template<class ForwardIterator>
void iterator_swap(ForwardIterator& left,ForwardIterator& right)
{
typename ForwardIterator::value_type tmp = *left;
*left = *right;
*right = tmp;
}
template<class T>
std::ostream& operator<<(std::ostream& out, const std::vector<T>& obj)
{
typename std::vector<T>::const_iterator beg = obj.cbegin();
typename std::vector<T>::const_iterator end = obj.cend();
while (beg != end)
{
out << *beg << '\n';
++beg;
}
return out;
}
int main()
{
vector<unsigned> v_1;
for (vector<unsigned>::size_type i = 0; i < 10; ++i)
{
v_1.push_back(i);
}
vector<unsigned> v_2;
for (vector<unsigned>::size_type i = 0; i < 10; ++i)
{
v_2.push_back(i*10);
}
cout << "v_1:\n" << v_1;
cout << "v_2:\n" << v_2;
iterator_swap(v_1.begin(),v_2.begin());
cout << "After swap:\n";
cout << "v_1:\n" << v_1;
cout << "v_2:\n" << v_2;
return 0;
}
В GCC я получаю следующее сообщение об ошибкеmsg:
E:\CodeBlocks\Iter_swap\main.cpp|41|error:
invalid initialization of non-const reference of type
'__gnu_cxx::__normal_iterator<unsigned int*, std::vector<unsigned int,
std::allocator<unsigned int> > >&' from a temporary of type
'__gnu_cxx::__normal_iterator<unsigned int*, std::vector<unsigned int,
std::allocator<unsigned int> > >'|
Какая команда права?VS или GCC?