在c++标准模板库(STL)中使用set
Using set in c++ standard template library (STL)
集就是集合。集合中有一个函数是insert()
.
它是 return 类型:它 return 是指向集合中插入元素的迭代器。
我写了一个代码及其工作原理:
#include<bits/stdc++.h>
using namespace std;
int main(){
set<int> s;
s.insert (1);
s.insert (4);
s.insert (2);
s.insert (5);
s.insert (3);
cout << "The elements in set are: ";
for (auto it = s.begin(); it!= s.end (); it++)
cout << *it;
return 0;
}
我写了另一个代码,它也能工作:
#include<bits/stdc++.h>
using namespace std;
int main(){
set<int> s;
auto itr=s.insert (s.begin(),5);
itr=s.insert (itr,4);
itr=s.insert (itr,2);
cout << "The elements in set are: ";
for (auto it = s.begin(); it!= s.end (); it++)
cout << *it;
return 0;
}
现在我合并了我之前代码的逻辑,但现在它不起作用,为什么? :
#include<bits/stdc++.h>
using namespace std;
int main(){
set<int> s;
auto itr=s.insert (1);
s.insert (itr,4);
cout << "The elements in set are: ";
for (auto it = s.begin(); it!= s.end (); it++)
cout << *it;
return 0;
}
以上代码输出:
/usr/include/c++/7/bits/stl_set.h:536:7: note: candidate: std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::insert(std::set<_Key, _Compare, _Alloc>::const_iterator, const value_type&) [with _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>; std::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<int>; std::set<_Key, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<int>; std::set<_Key, _Compare, _Alloc>::value_type = int]
insert(const_iterator __position, const value_type& __x)
^~~~~~
/usr/include/c++/7/bits/stl_set.h:536:7: note: no known conversion for argument 1 from ‘std::pair<std::_Rb_tree_const_iterator<int>, bool>’ to ‘std::set<int>::const_iterator {aka std::_Rb_tree_const_iterator<int>}’
/usr/include/c++/7/bits/stl_set.h:541:7: note: candidate: std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::insert(std::set<_Key, _Compare, _Alloc>::const_iterator, std::set<_Key, _Compare, _Alloc>::value_type&&) [with _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>; std::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<int>; std::set<_Key, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<int>; std::set<_Key, _Compare, _Alloc>::value_type = int]
insert(const_iterator __position, value_type&& __x)
^~~~~~
/usr/include/c++/7/bits/stl_set.h:541:7: note: no known conversion for argument 1 from ‘std::pair<std::_Rb_tree_const_iterator<int>, bool>’ to ‘std::set<int>::const_iterator {aka std::_Rb_tree_const_iterator<int>}’
/usr/include/c++/7/bits/stl_set.h:556:2: note: candidate: template<class _InputIterator> void std::set<_Key, _Compare, _Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator = _InputIterator; _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>]
insert(_InputIterator __first, _InputIterator __last)
^~~~~~
/usr/include/c++/7/bits/stl_set.h:556:2: note: template argument deduction/substitution failed:
yo.cpp:14:16: note: deduced conflicting types for parameter ‘_InputIterator’ (‘std::pair<std::_Rb_tree_const_iterator<int>, bool>’ and ‘int’)
s.insert (itr,4)
^
In file included from /usr/include/c++/7/set:61:0,
from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:87,
from yo.cpp:1:
/usr/include/c++/7/bits/stl_set.h:568:7: note: candidate: void std::set<_Key, _Compare, _Alloc>::insert(std::initializer_list<_Tp>) [with _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>]
insert(initializer_list<value_type> __l)
^~~~~~
/usr/include/c++/7/bits/stl_set.h:568:7: note: candidate expects 1 argument, 2 provided
当您使用语法时:
auto itr = s.insert(5);
带有参数5
的s.insert()
的return类型给出了一个类型:
std::pair<std::set<int>::iterator, bool>
但是语法中没有从该类型到 std::set<int>::iterator
的已知重载转换:
itr = s.insert(itr, 4); // error!
_______________^^^_____
要解决,需要正确声明itr
:
auto itr = s.insert(s.begin(), 5);
而不只是:
auto itr = s.insert(5);
在正确的情况下,s.insert()
的 return 类型是 std::set<int>::iterator
,它与下一个语句兼容。
集就是集合。集合中有一个函数是insert()
.
它是 return 类型:它 return 是指向集合中插入元素的迭代器。
我写了一个代码及其工作原理:
#include<bits/stdc++.h>
using namespace std;
int main(){
set<int> s;
s.insert (1);
s.insert (4);
s.insert (2);
s.insert (5);
s.insert (3);
cout << "The elements in set are: ";
for (auto it = s.begin(); it!= s.end (); it++)
cout << *it;
return 0;
}
我写了另一个代码,它也能工作:
#include<bits/stdc++.h>
using namespace std;
int main(){
set<int> s;
auto itr=s.insert (s.begin(),5);
itr=s.insert (itr,4);
itr=s.insert (itr,2);
cout << "The elements in set are: ";
for (auto it = s.begin(); it!= s.end (); it++)
cout << *it;
return 0;
}
现在我合并了我之前代码的逻辑,但现在它不起作用,为什么? :
#include<bits/stdc++.h>
using namespace std;
int main(){
set<int> s;
auto itr=s.insert (1);
s.insert (itr,4);
cout << "The elements in set are: ";
for (auto it = s.begin(); it!= s.end (); it++)
cout << *it;
return 0;
}
以上代码输出:
/usr/include/c++/7/bits/stl_set.h:536:7: note: candidate: std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::insert(std::set<_Key, _Compare, _Alloc>::const_iterator, const value_type&) [with _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>; std::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<int>; std::set<_Key, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<int>; std::set<_Key, _Compare, _Alloc>::value_type = int]
insert(const_iterator __position, const value_type& __x)
^~~~~~
/usr/include/c++/7/bits/stl_set.h:536:7: note: no known conversion for argument 1 from ‘std::pair<std::_Rb_tree_const_iterator<int>, bool>’ to ‘std::set<int>::const_iterator {aka std::_Rb_tree_const_iterator<int>}’
/usr/include/c++/7/bits/stl_set.h:541:7: note: candidate: std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::insert(std::set<_Key, _Compare, _Alloc>::const_iterator, std::set<_Key, _Compare, _Alloc>::value_type&&) [with _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>; std::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<int>; std::set<_Key, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<int>; std::set<_Key, _Compare, _Alloc>::value_type = int]
insert(const_iterator __position, value_type&& __x)
^~~~~~
/usr/include/c++/7/bits/stl_set.h:541:7: note: no known conversion for argument 1 from ‘std::pair<std::_Rb_tree_const_iterator<int>, bool>’ to ‘std::set<int>::const_iterator {aka std::_Rb_tree_const_iterator<int>}’
/usr/include/c++/7/bits/stl_set.h:556:2: note: candidate: template<class _InputIterator> void std::set<_Key, _Compare, _Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator = _InputIterator; _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>]
insert(_InputIterator __first, _InputIterator __last)
^~~~~~
/usr/include/c++/7/bits/stl_set.h:556:2: note: template argument deduction/substitution failed:
yo.cpp:14:16: note: deduced conflicting types for parameter ‘_InputIterator’ (‘std::pair<std::_Rb_tree_const_iterator<int>, bool>’ and ‘int’)
s.insert (itr,4)
^
In file included from /usr/include/c++/7/set:61:0,
from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:87,
from yo.cpp:1:
/usr/include/c++/7/bits/stl_set.h:568:7: note: candidate: void std::set<_Key, _Compare, _Alloc>::insert(std::initializer_list<_Tp>) [with _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>]
insert(initializer_list<value_type> __l)
^~~~~~
/usr/include/c++/7/bits/stl_set.h:568:7: note: candidate expects 1 argument, 2 provided
当您使用语法时:
auto itr = s.insert(5);
带有参数5
的s.insert()
的return类型给出了一个类型:
std::pair<std::set<int>::iterator, bool>
但是语法中没有从该类型到 std::set<int>::iterator
的已知重载转换:
itr = s.insert(itr, 4); // error!
_______________^^^_____
要解决,需要正确声明itr
:
auto itr = s.insert(s.begin(), 5);
而不只是:
auto itr = s.insert(5);
在正确的情况下,s.insert()
的 return 类型是 std::set<int>::iterator
,它与下一个语句兼容。