#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> years;
years.push_back(1983);
typedef std::vector<int>::const_iterator IterType;
IterType iter = std::find(static_cast<IterType>(years.begin()),
static_cast<IterType>(years.end()),
1983);
years.insert(static_cast<IterType>(iter),1998);
for(auto i = years.begin(); i != years.end();i++) {
std::cout << *i << std::endl;
}
}
-
c++98
编译报错.
-
g++ test.cpp -std=c++98
.
-
typedef
是c++98
版本的别名.
-
输出
[root@localhost test]# g++ test.cpp -std=c++98
test.cpp: In function ‘int main()’:
test.cpp:13:50: error: no matching function for call to ‘std::vector<int>::insert(__gnu_cxx::__normal_iterator<const int*, std::vector<int> >, int)’
years.insert(static_cast<IterType>(iter),1998);
^
test.cpp:13:50: note: candidates are:
In file included from /usr/include/c++/4.8.2/vector:69:0,
from test.cpp:3:
/usr/include/c++/4.8.2/bits/vector.tcc:107:5: note: std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(std::vector<_Tp, _Alloc>::iterator, const value_type&) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = int*; std::vector<_Tp, _Alloc>::value_type = int]
vector<_Tp, _Alloc>::
^
/usr/include/c++/4.8.2/bits/vector.tcc:107:5: note: no known conversion for argument 1 from ‘__gnu_cxx::__normal_iterator<const int*, std::vector<int> >’ to ‘std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}’
In file included from /usr/include/c++/4.8.2/vector:64:0,
from test.cpp:3:
/usr/include/c++/4.8.2/bits/stl_vector.h:1023:7: note: void std::vector<_Tp, _Alloc>::insert(std::vector<_Tp, _Alloc>::iterator, std::vector<_Tp, _Alloc>::size_type, const value_type&) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = int*; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = int]
insert(iterator __position, size_type __n, const value_type& __x)
^
/usr/include/c++/4.8.2/bits/stl_vector.h:1023:7: note: candidate expects 3 arguments, 2 provided
/usr/include/c++/4.8.2/bits/stl_vector.h:1050:9: note: template<class _InputIterator> void std::vector<_Tp, _Alloc>::insert(std::vector<_Tp, _Alloc>::iterator, _InputIterator, _InputIterator) [with _InputIterator = _InputIterator; _Tp = int; _Alloc = std::allocator<int>]
insert(iterator __position, _InputIterator __first,
^
/usr/include/c++/4.8.2/bits/stl_vector.h:1050:9: note: template argument deduction/substitution failed:
test.cpp:13:50: note: cannot convert ‘iter’ (type ‘__gnu_cxx::__normal_iterator<const int*, std::vector<int> >’) to type ‘std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}’
years.insert(static_cast<IterType>(iter),1998);
^
test.cpp:14:14: error: ‘i’ does not name a type
for(auto i = years.begin(); i != years.end();i++) {
^
test.cpp:14:33: error: expected ‘;’ before ‘i’
for(auto i = years.begin(); i != years.end();i++) {
^
test.cpp:14:33: error: ‘i’ was not declared in this scope
-
错误信息,insert
不能是const
类型.
-
核心是对迭代器的支持不够,c++98
则进行了加强.c++14
也进行了加强.
-
所以后面就推荐用迭代器,且const
类型的迭代器。