Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #85776
| From | Bo Persson <bo@bo-persson.se> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: map[string_view] not compiling |
| Date | 2022-08-01 14:32 +0200 |
| Message-ID | <jkpvfaF1b1gU1@mid.individual.net> (permalink) |
| References | <tc8epl$t0hn$1@dont-email.me> |
On 2022-08-01 at 13:52, Paavo Helde wrote:
>
> Hi all,
>
> Let's say I have a std::map with std::string keys and want to access it
> with std::string_view keys. For lookup there is a trick to add
> std::less<> to the map definition, but it appears this does not help
> with operator[] by some reason. Maybe some expert can clarify this?
>
> Here is an example:
>
> #include <map>
> #include <string>
> using namespace std::string_view_literals;
>
>
> int main() {
>
> std::string_view key = "foo"sv;
>
> std::map<std::string, int> a;
> //auto it = a.find(key); // not compiling
> // cannot convert argument 1 from 'std::string_view' to 'const
> std::basic_string ...
>
> std::map<std::string, int, std::less<>> b;
> auto it = b.find(key); // OK now
>
> b[key] = 3; // still not compiling
> // binary '[': no operator found which takes a right-hand operand
> of type 'std::string_view'
>
> b[std::string(key)] = 3; // OK now, but may construct a std::string
> without reason
>
> }
Not an expert, but operator[] takes a Key parameter, which is
std::string here. And string_view is not implicitly convertible to
std::string.
https://en.cppreference.com/w/cpp/container/map/operator_at
map::find is different in that it has extra overloads for types
comparable to Key.
template< class K > iterator find( const K& x );
https://en.cppreference.com/w/cpp/container/map/find
You enable those by using the generic std::less<> comparator, that
accepts different types for the two objects we want to compare.
Finally
b[std::string(key)] = 3;
works because it explicitly constructs a std::string matching the
key-type. We specifically don't want this by default, as it has the cost
of an extra construction. So that constructor is marked "explicit".
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
map[string_view] not compiling Paavo Helde <eesnimi@osa.pri.ee> - 2022-08-01 14:52 +0300
Re: map[string_view] not compiling Bo Persson <bo@bo-persson.se> - 2022-08-01 14:32 +0200
Re: map[string_view] not compiling Paavo Helde <eesnimi@osa.pri.ee> - 2022-08-01 17:11 +0300
Re: map[string_view] not compiling Öö Tiib <ootiib@hot.ee> - 2022-08-01 13:30 -0700
Re: map[string_view] not compiling Bo Persson <bo@bo-persson.se> - 2022-08-01 23:23 +0200
Re: map[string_view] not compiling Öö Tiib <ootiib@hot.ee> - 2022-08-01 22:52 -0700
Re: map[string_view] not compiling Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-08-04 08:31 -0700
Re: map[string_view] not compiling "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-08-03 11:34 +0200
Re: map[string_view] not compiling Juha Nieminen <nospam@thanks.invalid> - 2022-08-03 10:49 +0000
Re: map[string_view] not compiling "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-08-03 13:38 +0200
Re: map[string_view] not compiling Juha Nieminen <nospam@thanks.invalid> - 2022-08-04 06:16 +0000
Re: map[string_view] not compiling Paavo Helde <eesnimi@osa.pri.ee> - 2022-08-03 14:43 +0300
Re: map[string_view] not compiling "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-08-03 13:56 +0200
csiph-web