gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:20:06 27 4
gpt4 key购买 nike

集合是集合。 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com