English 中文(简体)
透明地寻找带有 st子的 map子,作为关键
原标题:Transparent search for a std map with a std pair as a key

If there is a std::map<std::pair<std::string, std::string>, some_type> what is the best way to find it s values?

I guess the most obvious one is to do something like this: map.find(std::make_pair(str1, str2)); but this will lead to a copy-construction of the str1 and str2 strings during the pair construction.

我希望,可能的话是map.find(std:make_pair(std:ref(str1), :ref(str2));可帮助,但不幸的是,这仍然产生印本。

map.find(std:make_pair(std:move(str1), :move(str2) 应当工作,但应当假设这些指示(str1,str2)是吻合的,或者不应移动。

So I m asking if there is any other way to do a the map search without making redundant string copies?

(请注意,使用<代码>std:string_view for the std:map: Key 是NOT的一种选择,因为地图应当有其示意图。 iii

最佳回答

C++14 added the following overload for std::map::find which allows transparent search:

template< class K >
const_iterator find( const K& x ) const;

为了加以利用,你仍然需要<代码>:地图,以便有一个适当的参照器:

struct pair_less {
    template <typename T, typename U>
      requires pair_less_than_comparable<T, U> // C++20, see below
    bool operator()(const T& a, const U& b) const {
        if (a.first < b.first) return true;
        if (b.first < a.first) return false;
        return a.second < b.second;
    }
};

int main() {
    std::map<std::pair<std::string, std::string>, int, pair_less> m { /* ... */ };
    // ...
    std::string a = "a", b = "b";
    // now works and creates no copies
    auto pos = m.find(std::make_pair(std::ref(a), std::ref(b)));
}

This pair_less is totally transparent. It can directly compare std::pair<X, Y> to std::pair<const X&, const Y&>.

Proper C++20 constraints

Properly constraining the call operator of pair_less is not so easy. It s not strictly necessary anyway, but moves the error to the call site of the operator and may be better for diagnostics.

通常的做法将沿用思路。

template <typename T, typename U>
concept pair_less_than_comparable
  = requires (const T& t, const U& u) {
      { t.first < u.first } -> /* boolean-testable */;
      { t.second < u.second } -> /* boolean-testable */;
  }

另见boolean-testable

问题回答

为避免主要施工,您可使用<代码>td:map,并有透明的参照器。 在许多情况下,你不需要执行自己的标准——该标准规定:std:less<>。 工作就是这样。

遗憾的是,它用<代码>td:pair赢得了t工作。 https://cplus.github.io/LWG/issue3865“rel=“nofollow noreferer”>lacks heterogeneous compars 您可以比较<代码>std:pair<std:string, :string>至std:pair<const :string & constpind:string &>

But if you could change std::pair to std::tuple, it will work:

std::map<std::tuple<std::string, std::string>, int, std::less<>> map;
// ...
auto pos = map.find(std::make_tuple(std::cref(str1), std::cref(str2)));




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签