删除C+中小于给定元素的元素+;设置 我有一个C++集。我想删除集合中小于指定值的所有元素。例如 std::set setElem; setElem.insert(1); setElem.insert(45); setElem.insert(47); //setElem contains {1,45,47} //delete all elements less than 46 such that the set now becomes: {47}
我知道我们可以通过验证元素是否小于46来迭代集合并删除元素。在C++中还有其他更有效的方法来做同样的事情吗?删除C+中小于给定元素的元素+;设置 我有一个C++集。我想删除集合中小于指定值的所有元素。例如 std::set setElem; setElem.insert(1); setElem.insert(45); setElem.insert(47); //setElem contains {1,45,47} //delete all elements less than 46 such that the set now becomes: {47},c++,C++,我知道我们可以通过验证元素是否小于46来迭代集合并删除元素。在C++中还有其他更有效的方法来做同样的事情吗? 我使用的gcc版本是:gcc(Ubuntu/Linaro 4.6.4-6ubuntu2)4.6.4您可以使用下限()和擦除() #包括 #包括 使用名称空间std; int main() { 塞塞特莱姆; 设置元素插入(1); 镶块(45); 镶块(46); 镶块(47); 镶块(50); 镶块(51); set::迭代器; it=设定元素下界(46); //擦除 setElem.era
我使用的gcc版本是:gcc(Ubuntu/Linaro 4.6.4-6ubuntu2)4.6.4您可以使用
下限()
和擦除()
#包括
#包括
使用名称空间std;
int main()
{
塞塞特莱姆;
设置元素插入(1);
镶块(45);
镶块(46);
镶块(47);
镶块(50);
镶块(51);
set::迭代器;
it=设定元素下界(46);
//擦除
setElem.erase(setElem.begin(),it);
//印刷品
for(set::iterator it1=setElem.begin();it1!=setElem.end();+it1)
coutuse std::set::lower_bound和std::set::Erase每当您需要这样的算法时,都值得一看。被否决的,因为您甚至懒得看一眼标准库引用的算法列表。@LightningRacisinObrit没错,问题是大多数人似乎仍然不知道核心语言和标准库。把查找标准库的地方放在一边;许多人仍然访问错误的cplusplus.com:(
#include <set>
#include <iostream>
using namespace std;
int main()
{
set<int>setElem;
setElem.insert(1);
setElem.insert(45);
setElem.insert(46);
setElem.insert(47);
setElem.insert(50);
setElem.insert(51);
set<int>:: iterator it;
it=setElem.lower_bound(46);
//Erasing
setElem.erase(setElem.begin(),it);
//Printing
for(set<int>::iterator it1=setElem.begin(); it1!=setElem.end(); ++it1)
cout<< *it1 << "\n";
}