c++ - 在c++标准模板库(STL)中使用set

标签 c++ stl insert set

集合是集合。 set中有一个函数是insert()

它的返回类型:它返回指向集合中插入元素的迭代器。

我写了一段代码及其工作原理:

#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);

s.insert() 的返回类型使用参数 5给出一种类型:

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() 的返回类型是std::set<int>::iterator这与下一个语句兼容。

关于c++ - 在c++标准模板库(STL)中使用set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63050845/

相关文章:

c++ - n 个元素中 k 个元素的所有组合

c++ - 我应该使用哪个数据集?

mysql - 当列数与值数不匹配时插入

sql - EntityFramework,不存在则插入,否则更新

C++ 内存泄漏

c++ - 单元测试++和主要

C++ stof函数转换为整数

c++ - 为什么C++空类没有字节对齐?

c++ - std::list<std::shared_ptr>::erase 得到一个 SIGSEGV

mysql - CONCATENATED INSERT 不接受 WHERE 子句