我有一个std::list< std::pair<std::string,double> >,我知道按照分类排序std::string element.
因为我希望做了很多std::find_if基于对std::string元素,相信std::map<string,double,MyOwnBinaryPredicate>与lower_bound和upper_bound会更充足.
事实是,我希望以有效的方式使用insert元素std::map.所以我想使用额外的迭代器来insert加快速度.
我认为,最简单的方法是使用一个const_reverse_iterator要经过std::list和使用begin()的std::map.
你会这样做,还是一个坏主意?
谢谢!
为什么我不能从一对中返回unique_ptr?
#include <iostream>
#include <memory>
#include <utility>
using namespace std;
unique_ptr<int> get_value() {
pair<unique_ptr<int>, int> p(unique_ptr<int>(new int(3)), 4);
return p.first;
}
int main(void) {
cout << *get_value() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试用g ++ 4.6编译它时,我得到:
../main.cpp: In function ‘std::unique_ptr<int> get_value()’:
../main.cpp:9:11: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int, _Dp = std::default_delete<int>, std::unique_ptr<_Tp, _Dp> = std::unique_ptr<int>]’
/usr/include/c++/4.6/bits/unique_ptr.h:256:7: error: declared here
make: *** [main.o] Error 1
Run Code Online (Sandbox Code Playgroud)
我不明白错误信息
虽然我不喜欢它,但发现它inconvinient声明一个pair<X,Y>对象,或请调用make_pair,以调用map::insert.为什么insert不分别使用两个参数来指定Key和Value.
虽然我知道这是为了与其他STL容器兼容,但是展示了这一点value_type.但是find方法会key_type破坏这种兼容性断言.map既有key_type和mapped_type,所以为什么不能map有:
iterator insert(const key_type&, const mapped_type&);
Run Code Online (Sandbox Code Playgroud)
是的,有insert迭代器的重载.但这两个论点insert可能会很好.
我看到的一个优点是:减少了调用堆栈的使用.
编辑:刚刚发现这insert是唯一采用的方法value_type,即pair<X,Y>.许多其他的方法,如find,erase,at,count,equal_range,lower_bound,upper_bound和operator[]拿key_type.
我希望能够std::pair在unordered_container中使用a 作为键.我知道我可以通过以下方式执行此操作:
template<typename T>
void
hash_combine(std::size_t &seed, T const &key) {
std::hash<T> hasher;
seed ^= hasher(key) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
namespace std {
template<typename T1, typename T2>
struct hash<std::pair<T1, T2>> {
std::size_t operator()(std::pair<T1, T2> const &p) const {
std::size_t seed(0);
::hash_combine(seed, p.first);
::hash_combine(seed, p.second);
return seed;
}
};
}
Run Code Online (Sandbox Code Playgroud)
但是,我希望散列忽略元素的顺序std::pair(即,为std::pair<A, B>和返回相同的种子)std::pair<B, A>).
我认为实现这一目标的一种方法是在创建我的时候应用某种排序std::pair<A, B>(即某种自定义std::make_pair).但由于对象A, B可能没有订单,因此限制性太强.
是否有一种标准的方法来散列a std::pair,这样就忽略了元素的顺序,并且返回相同的种子 …
所以我有一套 pairs<string ,string>
我想用来find()搜索一对字符串,这个字符串位于该对的"第一个"中,然后如果我首先找到该字符串,我想从该函数返回第二个字符串.
我目前的尝试是......
myList::iterator i;
i = theList.find(make_pair(realName, "*"));
return i->second;
Run Code Online (Sandbox Code Playgroud) 在使用 std::pair 时,我遇到了两种不同的方法来访问其元素。由于它们似乎都有效且有效,我想知道它们之间有什么区别,哪种方法是首选?
std::pair<int, int> p(1,1); // can be of any type.
int i1 = p.first; // first approach
int i2 = std::get<0>(p); // second approach
Run Code Online (Sandbox Code Playgroud) #include <bits/stdc++.h>
std::unordered_map<std::pair<int,int>, int> mp;
int main()
{
mp[make_pair(1, 2)]++;
}
Run Code Online (Sandbox Code Playgroud)
使用时[] operator,我得到了这个
error: no match for ‘operator[]’ (operand types are ‘std::unordered_map<std::pair<int, int>, int>’ and ‘std::pair<int, int>’)
Run Code Online (Sandbox Code Playgroud)
但是,当用 做同样的事情时std::map,不会发生错误。为什么?
我怎样才能让它工作std::unorderd_m?
我有 C++11 程序,它需要从一系列值中选择一个值。每个范围都有一个指定的值以从中选择一个值。
以下是我的值范围,并为每个值分配了值。
1-10 = 5
11-14 = 13
15-20 = 17
21-28 = 24
29-36 = 31
37-47 = 43
48-52 = 50
53-60 = 53
61-68 = 65
Run Code Online (Sandbox Code Playgroud)
因此,如您所见,如果输入介于 1-10 之间,则 5 应为返回值,如果输入介于 11-14 之间,则应选择 13,依此类推。
上面的要求可以通过下面的程序来实现,其中包含一个很大的 if else 语句的脏列表。
#include <iostream>
// Following are return values to chosen based on which range the input value falls within
// 1-10 = 5
// 11-14 = 13
// 15-20 = 17
// 21-28 = 24
// 29-36 = …Run Code Online (Sandbox Code Playgroud) 我有一个函数f(),它返回std::pair<A, B>具有某些类型A和B. 我还有另一个函数g(),它调用f()两次并返回一个std::tuple<A, B, A, B>. 有没有办法tuple直接从两个调用构造返回f()?所以我想将当前代码的最后三行作为快捷方式:
std::tuple<A, B, A, B>
g(type1 arg1, type2 arg2, ...) {
// perform some preprocessing
auto pair1 = f(...);
auto pair2 = f(...);
return { pair1.first, pair1.second, pair2.first, pair2.second }
}
Run Code Online (Sandbox Code Playgroud)
成一个班轮(而不是三个)。如果该解决方案也适用于任意长度的元组,那就太好了,尤其是在模板情况下。
例如,在当前的 Python3 中,解决方案是return (*f(...), *f(...)). C++ 中是否有与 Python 中类似的列表/元组解包运算符?