-
主要内容
-
const
迭代器-
什么时候推荐
-
C++98
-
C++98
缺陷- 创建复杂,使用麻烦,功能有限.
-
C++98
的需求- 从一个
std::vector<int>
中搜索1983
,然后在其后面插入1998
. - 没有找到则插在末尾.
- 从一个
-
实现
#include <iostream> #include <algorithm> #include <vector> int main() { std::vector<int> years {1983}; auto iter = std::find(years.begin(),years.end(),1983); years.insert(iter + 1,1998); for(auto i = years.begin(); i != years.end();i++) { std::cout << *i << std::endl; } }
-
C++11
#include <iostream> #include <algorithm> #include <vector> int main() { std::vector<int> years {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
版本#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; } }
[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
-
核心观念
-
-
c++11
-
需求
-
更加通用的函数
-
改成通用型
#include <iostream> #include <algorithm> #include <vector> template<typename Container,typename T = typename Container::value_type> void insert(Container && years,T val) { auto iter = std::find(std::cbegin(years),std::cend(years),val); years.insert(iter,1998); for(auto i = years.cbegin(); i != years.cend();i++) { std::cout << *i << std::endl; } } int main() { std::vector<int> t{1983}; insert(t,1); }
-
c++11
#include <iostream> #include <algorithm> #include <vector> template<typename T> auto my_cbegin(const T& t) -> decltype(std::begin(t)) { return std::begin(t); } template<typename T> auto my_cend(const T& t) -> decltype(std::end(t)) { return std::end(t); } template<typename Container,typename T = typename Container::value_type> void insert(Container && years,T val) { auto iter = std::find(years.begin(),years.end(),val); years.insert(iter,1998); for(auto i = my_cbegin(years); i != my_cend(years);i++) { std::cout << *i << std::endl; } } int main() { std::vector<int> t{1983}; insert(t,1); }
-
c++14
版本#include <iostream> #include <algorithm> #include <vector> template<typename T> auto my_cbegin(const T& t) -> decltype(std::begin(t)) { return std::begin(t); } template<typename T> auto my_cend(const T& t) -> decltype(std::end(t)) { return std::end(t); } template<typename Container,typename T = typename Container::value_type> void insert(Container && years,T val) { auto iter = std::find(my_cbegin(years),my_cend(years),val); years.insert(iter,1998); for(auto i = my_cbegin(years); i != my_cend(years);i++) { std::cout << *i << std::endl; } } int main() { std::vector<int> t{1983}; insert(t,1); }
-
三方版
#include <iostream> #include <algorithm> #include <vector> class myType { int a[10]{}; public: auto begin() const { return a; } auto end() const { return a+10; } auto insert(const int*,int b) { a[0]+=b; return a; } }; template<typename T> auto my_cbegin(const T& t) -> decltype(std::begin(t)) { return std::begin(t); } template<typename T> auto my_cend(const T& t) -> decltype(std::end(t)) { return std::end(t); } template<typename Container,typename T = typename Container::value_type> void insert(Container && years,T val) { auto iter = std::find(my_cbegin(years),my_cend(years),val); years.insert(iter,1998); for(auto i = my_cbegin(years); i != my_cend(years);i++) { std::cout << *i << std::endl; } } int main() { // std::vector<int> t{1983}; // insert(t,1); myType t; insert(t,1); }
-
-
总结
C++11 推荐只要可以,尽量用const类型的迭代器
最新推荐文章于 2025-05-02 19:59:58 发布