我正在使用一个集合因为,我想使用已排序容器(如集合)的快速查找属性.我想知道是否必须使用find成员方法来获取已排序容器的好处,还是我还可以在STL算法中使用静态find方法?
我的预感是使用静态版本将使用线性搜索而不是我想要的二进制搜索.
为什么这段代码会导致运行时错误"vector iterator not incrementable"?
vector<string> s1, s2;
s1.push_back("joe");
s1.push_back("steve");
s1.push_back("jill");
s1.push_back("svetlana");
s2.push_back("bob");
s2.push_back("james");
s2.push_back("jill");
s2.push_back("barbara");
s2.push_back("steve");
sort(s1.begin(), s1.end());
sort(s2.begin(), s2.end());
vector<string> result;
vector<string>::iterator it_end, it_begin;
it_end = set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), result.begin());
cout << int (it_end - result.begin()) << endl;
for_each(result.begin(), result.end(), print);
Run Code Online (Sandbox Code Playgroud) 是否有STL实用程序/算法可以delete *the_object_iterator;对所有对象执行操作?这样我可以clear()安全吗?STL容器是a set,对象是指向用其创建的C++类的指针new.
Boost似乎是最好的解决方案.我的目标是避免在不可复制的类上进行复制构造.
我在一个结构中有一个函数,用于对结构中的向量进行排序.但是为了比较向量中的两个元素,我需要在同一个结构中的另一个变量的值.我想知道我应该在哪里保持运算符重载或比较函数这种工作.我在下面的粘贴中给出了一个样本.
#include<vector>
#include<algorithm>
struct Square{
int color; //value 1 to 10
};
struct State{
vector<Square> list;
int color_weight[] = {4,3,5,2,4,1,6,4,5,9}; //These values keep changing.
bool operator<(Square& a, Square& b);
void sortTheList();
};
bool State::operator<(Square& a, Square& b){
if (color_weight[a.color]< color_weight[b.color]){
return true;
}
return false;
}
void Square::sortTheList(){
sort(list.begin(),list.end());
}
Run Code Online (Sandbox Code Playgroud)
当然,这不起作用.我已经尝试了许多其他签名和范围的比较功能,但似乎没有任何工作.
知道在这里可以做些什么吗?
假设我们想要从ints 的向量中删除重复值.通常的解决方案是使用擦除 - 移除习语对矢量进行排序并擦除重复项.但我们需要保留不会被删除的元素的顺序,所以我们无法排序.所以有人可能会提出这样的谓词并与remove_if算法一起使用:
struct comp {
std::set<int> s;
comp() : s() {}
bool operator()(int i)
{
return !(s.insert(i)).second;
}
};
Run Code Online (Sandbox Code Playgroud)
但是如果出于某种原因将复制谓词对象,这将会中断,因为我们将获得该set成员的两个副本.事实上,gcc的实现remove_if确实如此:
template<typename _ForwardIterator, typename _Predicate>
_ForwardIterator
remove_if(_ForwardIterator __first, _ForwardIterator __last,
_Predicate __pred)
{
__first = _GLIBCXX_STD_A::find_if(__first, __last, __pred);
if(__first == __last) // ^^^^^ here a copy is made
return __first;
_ForwardIterator __result = __first;
++__first;
for(; __first != __last; ++__first)
if(!bool(__pred(*__first)))
{
*__result = _GLIBCXX_MOVE(*__first);
++__result;
}
return __result;
}
Run Code Online (Sandbox Code Playgroud)
解决方法是使 …
你可以在下面的代码中向我解释一下我做错了什么吗?我希望第二个向量中的值> = 80,但它是空的.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Tester
{
public:
int value;
Tester(int foo)
{
value = foo;
}
};
bool compare(Tester temp)
{
if (temp.value < 80)
return true;
else
return false;
}
int main()
{
vector<Tester> vec1;
vector<Tester> vec2;
vec1.reserve(100);
vec2.reserve(100);
for(int foo=0; foo<100; ++foo)
vec1.push_back(Tester(foo));
remove_copy_if(vec1.begin(), vec1.end(), vec2.begin(), compare);
cout<< "Size: " << vec2.size() << endl;
cout<< "Elements"<<endl;
for(int foo=0; foo < vec2.size(); ++foo)
cout << vec2.at(foo).value << " ";
cout<<endl; …Run Code Online (Sandbox Code Playgroud) 考虑以下两种方法将元素附加到向量中
std::vector<int> vi1(10,42), vi2;
vi2.insert(vi2.end(),vi1.begin(),vi1.end());
<OR>
std::copy(vi1.begin(),vi1.end(),std::back_inserter(vi2));
Run Code Online (Sandbox Code Playgroud)
std::copy版本看起来更干净,我不必输入vi2两次.但是因为它是一个通用的算法,而insert是一个成员函数,可能insert比std::copy它做得更好或者做同样的事情吗?
我可以自己测试,但我必须为每个模板类型的每个向量执行此操作.有人做过吗?
#include <iostream>
#include <random>
#include <algorithm>
int main()
{
std::mt19937 mt;
std::uniform_int_distribution<int> d(0, 255);
int l = 500;
std::vector<int> k(l);
std::generate(k.begin(), k.end(), d(mt));
return(0);
}
Run Code Online (Sandbox Code Playgroud)
试图与这两个编译g++ 4.7.2和g++ 4.8.1像这样
g++ -std=c++11 main.cpp
Run Code Online (Sandbox Code Playgroud)
报告的错误是
In file included from /usr/include/c++/4.7/algorithm:63:0,
from main.cpp:3:
/usr/include/c++/4.7/bits/stl_algo.h: In instantiation of ‘void std::generate(_FIter, _FIter, _Generator) [with _FIter = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Generator = int]’:
main.cpp:11:42: required from here
/usr/include/c++/4.7/bits/stl_algo.h:5083:2: error: ‘__gen’ cannot be used as a function
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我没有任何关于可能出错的线索或想法.
有没有办法将非修改标准库算法应用于离散函数而不是容器?
例如,请考虑以下功能
int sqr(int i)
{
return i*i;
}
Run Code Online (Sandbox Code Playgroud)
我如何使用std::find或std::lower_bound搜索值49,即算法应该返回7?最简单的方法是将返回值放入向量并将算法应用于向量 - 但这显然效率低下.
我正在搜索一个可以计算所有正数的函数vector!我需要你的帮助.到目前为止,我发现的唯一功能是std::count()来自algorithm,但它只搜索容器中相当于某个值的元素.也许有一种方法可以使这个函数在一定范围内搜索匹配(在我的情况下,这个范围将从1到+无穷大)?谢谢.