在c++中使用STL搜索变量/项目/属性
Search variable/item/attribute using STL in c++?
有任何方法可以使用STL在C++中搜索项、属性或变量
我们可以使用STL的任何提供Searching time as less as possible
的容器。容器包含pair<int,int>
。我想搜索应该返回其CCD_ 4的所有对X的对CCD_
例如
让容器处于无序设置状态。
unordered_set< pair<int , int > > myset =
{{1,2},{1,5},{1,6},{2,4},{3,5},{4,6},{6,7},{6,8}};
if i search for p(1,5) then it should return pair(1,2),(1,6)
if i search for p(2,4) or (3,5),(6,7) then it should return NULL i.e. nothing
if i search for p(6,7) then it should return pair(6,8)
类似的东西
std::vector<std::pair<int, int>>
find_nonmatching_values(const std::unordered_multimap<int, int> & thing,
int key, int value) {
std::vector<std::pair<int, int>> ret;
auto range = thing.equal_range(key);
std::copy_if(range.first, range.second, std::back_inserter(ret),
[value](const std::pair<const int, int> &p)
{ return p.second != value; });
return ret;
}
演示。将此代码模板化是留给读者的练习。
比T.C.版本更通用:
#include <type_traits>
#include <iterator>
template <typename T, typename InputIterator, typename OutputIterator, typename Comparator>
void find_mismatches(InputIterator first, InputIterator last,
T const& val, OutputIterator out, Comparator comp)
{
for (; first != last; ++first)
{
auto&& f = *first;
if (!comp(f.second, val))
*out++ = f;
}
}
template <typename AssociativeCont, typename OutputIterator, typename Comparator>
void find_mismatches(AssociativeCont&& rng, typename std::remove_reference<AssociativeCont>::type::value_type const& val, OutputIterator out, Comparator comp)
{
auto range = rng.equal_range(val.first);
find_mismatches(range.first, range.second, val.second, out, comp);
}
template <typename AssociativeCont, typename OutputIterator>
void find_mismatches(AssociativeCont&& rng, typename std::remove_reference<AssociativeCont>::type::value_type const& val, OutputIterator out)
{
auto range = rng.equal_range(val.first);
find_mismatches(range.first, range.second, val.second, out, std::equal_to<decltype(val.second)>());
}
演示。请注意,您仍然可以通过使用一个模板参数来扩展它,该参数是指向value_type
成员的成员的指针。
相关文章:
- C++不正确,不需要重新声明类成员变量 MFC 手工解决方案/项目 MS VS 2015
- 在 Visual Studio 2017 C++项目中设置调试/运行环境变量
- 我们是否应该在项目中包含的所有文件中声明外部变量
- 如何在运行qmake时将变量传递到qmake项目文件
- 我正在尝试在Eclipse中构建一个C 项目,但是即使在使用MINGW路径设置路径变量之后,也会获得构建错误
- 使用ROS2环境变量创建C 项目
- 在两个项目中使用全局变量
- 我应该如何在我的C++项目中声明全局变量
- 可以在项目的任何文件中使用的C++变量
- Eclipse C++项目未解析LD_LIBRARY_PATH包含变量
- C++/C在Visual Studio中的两个项目之间共享全局变量
- 如何在代码中使用 Eclipse 项目变量
- C++:使用宏字符串将项目转换为动态变量的简单方法
- 在可执行项目中执行库的全局变量生成部分
- LPCSTR 在 CLR 项目中创建未声明的变量
- 在c++中使用STL搜索变量/项目/属性
- Cmake错误:项目中使用了以下变量,但它们被设置为NOTFOUND,试图在archlinux中编译Rigs of Ro
- 在Eclipse中访问c++项目中的Bison变量
- 如何创建一个具有全局变量的 lib 文件,这些全局变量应该将其信息共享给另一个项目
- 如何在 Visual C++ 中的项目之间共享代码,如何在静态库中的静态变量"unresolved external symbol"共享代码