Copy std::map into std::vector of pairs
Clash Royale CLAN TAG#URR8PPP
I'm trying to copy a map into a vector of pair, so I can then sort the vector by the second
data member of the pairs. I have resolved this doing like this:
void mappedWordsListSorter()
for (auto itr = mappedWordsList.begin(); itr != mappedWordsList.end(); ++itr)
vectorWordsList.push_back(*itr);
sort(vectorWordsList.begin(), vectorWordsList.end(), [=](pair<string, int>& a, pair<string, int>& b)return a.second > b.second;);
I need to find a way to do this without using a raw loop, using the standard library instead. I have come across a lot of examples doing this by only transferring either the keys or the values of the map. I need to copy into a vector of pairs<string, int>
. What is the best way to do it?
c++ stdvector stdmap c++-standard-library std-pair
|
show 4 more comments
I'm trying to copy a map into a vector of pair, so I can then sort the vector by the second
data member of the pairs. I have resolved this doing like this:
void mappedWordsListSorter()
for (auto itr = mappedWordsList.begin(); itr != mappedWordsList.end(); ++itr)
vectorWordsList.push_back(*itr);
sort(vectorWordsList.begin(), vectorWordsList.end(), [=](pair<string, int>& a, pair<string, int>& b)return a.second > b.second;);
I need to find a way to do this without using a raw loop, using the standard library instead. I have come across a lot of examples doing this by only transferring either the keys or the values of the map. I need to copy into a vector of pairs<string, int>
. What is the best way to do it?
c++ stdvector stdmap c++-standard-library std-pair
1
@Lorand - The sort happens on thesecond
item in the pair, which is not the key.
– StoryTeller
Dec 19 '18 at 15:37
1
@Lorand It seems that OP wants to perform the sort based on the values, not the keys.
– ネロク
Dec 19 '18 at 15:38
3
Seems like that question should be flagged as a duplicate of this one, since the answer provided by @NathanOliver is way better than any of the answers to that question. Edit : Though that question includes sorting the result.
– François Andrieux
Dec 19 '18 at 15:40
1
@ALX23z - I agree that[=]
should not be present. But just because it's there doesn't mean everything gets copied. The lambda has to use something from the surrounding scope to capture it.
– StoryTeller
Dec 19 '18 at 15:46
1
@ALX23z Actually, it will only copy a variable is you use it in the lambda body. There is no performance penalty to use[=]
if you don't use any of the variables in scope.
– NathanOliver
Dec 19 '18 at 15:46
|
show 4 more comments
I'm trying to copy a map into a vector of pair, so I can then sort the vector by the second
data member of the pairs. I have resolved this doing like this:
void mappedWordsListSorter()
for (auto itr = mappedWordsList.begin(); itr != mappedWordsList.end(); ++itr)
vectorWordsList.push_back(*itr);
sort(vectorWordsList.begin(), vectorWordsList.end(), [=](pair<string, int>& a, pair<string, int>& b)return a.second > b.second;);
I need to find a way to do this without using a raw loop, using the standard library instead. I have come across a lot of examples doing this by only transferring either the keys or the values of the map. I need to copy into a vector of pairs<string, int>
. What is the best way to do it?
c++ stdvector stdmap c++-standard-library std-pair
I'm trying to copy a map into a vector of pair, so I can then sort the vector by the second
data member of the pairs. I have resolved this doing like this:
void mappedWordsListSorter()
for (auto itr = mappedWordsList.begin(); itr != mappedWordsList.end(); ++itr)
vectorWordsList.push_back(*itr);
sort(vectorWordsList.begin(), vectorWordsList.end(), [=](pair<string, int>& a, pair<string, int>& b)return a.second > b.second;);
I need to find a way to do this without using a raw loop, using the standard library instead. I have come across a lot of examples doing this by only transferring either the keys or the values of the map. I need to copy into a vector of pairs<string, int>
. What is the best way to do it?
c++ stdvector stdmap c++-standard-library std-pair
c++ stdvector stdmap c++-standard-library std-pair
edited Dec 19 '18 at 20:49
Deduplicator
34k64787
34k64787
asked Dec 19 '18 at 15:33
Victor
862
862
1
@Lorand - The sort happens on thesecond
item in the pair, which is not the key.
– StoryTeller
Dec 19 '18 at 15:37
1
@Lorand It seems that OP wants to perform the sort based on the values, not the keys.
– ネロク
Dec 19 '18 at 15:38
3
Seems like that question should be flagged as a duplicate of this one, since the answer provided by @NathanOliver is way better than any of the answers to that question. Edit : Though that question includes sorting the result.
– François Andrieux
Dec 19 '18 at 15:40
1
@ALX23z - I agree that[=]
should not be present. But just because it's there doesn't mean everything gets copied. The lambda has to use something from the surrounding scope to capture it.
– StoryTeller
Dec 19 '18 at 15:46
1
@ALX23z Actually, it will only copy a variable is you use it in the lambda body. There is no performance penalty to use[=]
if you don't use any of the variables in scope.
– NathanOliver
Dec 19 '18 at 15:46
|
show 4 more comments
1
@Lorand - The sort happens on thesecond
item in the pair, which is not the key.
– StoryTeller
Dec 19 '18 at 15:37
1
@Lorand It seems that OP wants to perform the sort based on the values, not the keys.
– ネロク
Dec 19 '18 at 15:38
3
Seems like that question should be flagged as a duplicate of this one, since the answer provided by @NathanOliver is way better than any of the answers to that question. Edit : Though that question includes sorting the result.
– François Andrieux
Dec 19 '18 at 15:40
1
@ALX23z - I agree that[=]
should not be present. But just because it's there doesn't mean everything gets copied. The lambda has to use something from the surrounding scope to capture it.
– StoryTeller
Dec 19 '18 at 15:46
1
@ALX23z Actually, it will only copy a variable is you use it in the lambda body. There is no performance penalty to use[=]
if you don't use any of the variables in scope.
– NathanOliver
Dec 19 '18 at 15:46
1
1
@Lorand - The sort happens on the
second
item in the pair, which is not the key.– StoryTeller
Dec 19 '18 at 15:37
@Lorand - The sort happens on the
second
item in the pair, which is not the key.– StoryTeller
Dec 19 '18 at 15:37
1
1
@Lorand It seems that OP wants to perform the sort based on the values, not the keys.
– ネロク
Dec 19 '18 at 15:38
@Lorand It seems that OP wants to perform the sort based on the values, not the keys.
– ネロク
Dec 19 '18 at 15:38
3
3
Seems like that question should be flagged as a duplicate of this one, since the answer provided by @NathanOliver is way better than any of the answers to that question. Edit : Though that question includes sorting the result.
– François Andrieux
Dec 19 '18 at 15:40
Seems like that question should be flagged as a duplicate of this one, since the answer provided by @NathanOliver is way better than any of the answers to that question. Edit : Though that question includes sorting the result.
– François Andrieux
Dec 19 '18 at 15:40
1
1
@ALX23z - I agree that
[=]
should not be present. But just because it's there doesn't mean everything gets copied. The lambda has to use something from the surrounding scope to capture it.– StoryTeller
Dec 19 '18 at 15:46
@ALX23z - I agree that
[=]
should not be present. But just because it's there doesn't mean everything gets copied. The lambda has to use something from the surrounding scope to capture it.– StoryTeller
Dec 19 '18 at 15:46
1
1
@ALX23z Actually, it will only copy a variable is you use it in the lambda body. There is no performance penalty to use
[=]
if you don't use any of the variables in scope.– NathanOliver
Dec 19 '18 at 15:46
@ALX23z Actually, it will only copy a variable is you use it in the lambda body. There is no performance penalty to use
[=]
if you don't use any of the variables in scope.– NathanOliver
Dec 19 '18 at 15:46
|
show 4 more comments
3 Answers
3
active
oldest
votes
Just use std::vector
's assign
member function.
//no need to call reserve, bidirectional iterators or better will compute the size and reserve internally.
vectorWordsList.assign(mappedWordsList.begin(), mappedWordsList.end());
If you have existing values in the vector that you don't want overwritten then use insert
instead like
vectorWordsList.reserve(vectorWordsList.size() + mappedWordsList.size()); // make sure we only have a single memory allocation
vectorWordsList.insert(vectorWordsList.end(), mappedWordsList.begin(), mappedWordsList.end());
1
I would hope thatassign
would essentiallyreserve
internally.
– François Andrieux
Dec 19 '18 at 15:41
10
@FrançoisAndrieux It most likely would if you passed random access iterators but sincemap
has bidirectional iterators it requires a traversal to known how many elements to allocate space for so it isn't going to do that.
– NathanOliver
Dec 19 '18 at 15:43
1
@NathanOliver libstdc++ actually does essentiallyreserve
internally. For any ForwardIterator, it will callstd::distance
, paying for a traversal for anything less than a RandomAccessIterator. You can tracestd::vector::assign
to here.
– Justin
Dec 19 '18 at 19:43
1
@Justin Cool. Thanks for finding that. I still like to be explicit, in case other implementations don't do this.
– NathanOliver
Dec 19 '18 at 19:47
Putting a.resize(0)
before.reserve()
might be an optimization if the elements should be replaced.
– Deduplicator
Dec 19 '18 at 20:46
|
show 2 more comments
You can use std::copy
and std::back_inserter
:
std::copy(mappedWordsList.begin(),
mappedWordsList.end(),
std::back_inserter(vectorWordsList));
Honestly, I think that a range-for
loop is clearer:
for(const auto& kv : mappedWordsList)
vectorWordsList.emplace_back(kv);
Regardless, you can use std::vector::reserve
to preallocate memory on your target vector
, avoiding unnecessary reallocations.
add a comment |
It's worth noting that if you are creating a vector for this purpose, you may use the vector's constructor directly:
std::vector<std::pair<FirstType,SecondType>> vectorWordsList( mappedWordsList.begin(), mappedWordsList.end() );
In C++17, you may also omit the vector's template arguments to have the compiler deduce them:
std::vector vectorWordsList( mappedWordsList.begin(), mappedWordsList.end() );
Unless you want to create a vector of iterators, you need parens.
– T.C.
Dec 20 '18 at 12:00
@T.C. thank you!
– Drew Dormann
Dec 20 '18 at 16:18
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53854468%2fcopy-stdmap-into-stdvector-of-pairs%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Just use std::vector
's assign
member function.
//no need to call reserve, bidirectional iterators or better will compute the size and reserve internally.
vectorWordsList.assign(mappedWordsList.begin(), mappedWordsList.end());
If you have existing values in the vector that you don't want overwritten then use insert
instead like
vectorWordsList.reserve(vectorWordsList.size() + mappedWordsList.size()); // make sure we only have a single memory allocation
vectorWordsList.insert(vectorWordsList.end(), mappedWordsList.begin(), mappedWordsList.end());
1
I would hope thatassign
would essentiallyreserve
internally.
– François Andrieux
Dec 19 '18 at 15:41
10
@FrançoisAndrieux It most likely would if you passed random access iterators but sincemap
has bidirectional iterators it requires a traversal to known how many elements to allocate space for so it isn't going to do that.
– NathanOliver
Dec 19 '18 at 15:43
1
@NathanOliver libstdc++ actually does essentiallyreserve
internally. For any ForwardIterator, it will callstd::distance
, paying for a traversal for anything less than a RandomAccessIterator. You can tracestd::vector::assign
to here.
– Justin
Dec 19 '18 at 19:43
1
@Justin Cool. Thanks for finding that. I still like to be explicit, in case other implementations don't do this.
– NathanOliver
Dec 19 '18 at 19:47
Putting a.resize(0)
before.reserve()
might be an optimization if the elements should be replaced.
– Deduplicator
Dec 19 '18 at 20:46
|
show 2 more comments
Just use std::vector
's assign
member function.
//no need to call reserve, bidirectional iterators or better will compute the size and reserve internally.
vectorWordsList.assign(mappedWordsList.begin(), mappedWordsList.end());
If you have existing values in the vector that you don't want overwritten then use insert
instead like
vectorWordsList.reserve(vectorWordsList.size() + mappedWordsList.size()); // make sure we only have a single memory allocation
vectorWordsList.insert(vectorWordsList.end(), mappedWordsList.begin(), mappedWordsList.end());
1
I would hope thatassign
would essentiallyreserve
internally.
– François Andrieux
Dec 19 '18 at 15:41
10
@FrançoisAndrieux It most likely would if you passed random access iterators but sincemap
has bidirectional iterators it requires a traversal to known how many elements to allocate space for so it isn't going to do that.
– NathanOliver
Dec 19 '18 at 15:43
1
@NathanOliver libstdc++ actually does essentiallyreserve
internally. For any ForwardIterator, it will callstd::distance
, paying for a traversal for anything less than a RandomAccessIterator. You can tracestd::vector::assign
to here.
– Justin
Dec 19 '18 at 19:43
1
@Justin Cool. Thanks for finding that. I still like to be explicit, in case other implementations don't do this.
– NathanOliver
Dec 19 '18 at 19:47
Putting a.resize(0)
before.reserve()
might be an optimization if the elements should be replaced.
– Deduplicator
Dec 19 '18 at 20:46
|
show 2 more comments
Just use std::vector
's assign
member function.
//no need to call reserve, bidirectional iterators or better will compute the size and reserve internally.
vectorWordsList.assign(mappedWordsList.begin(), mappedWordsList.end());
If you have existing values in the vector that you don't want overwritten then use insert
instead like
vectorWordsList.reserve(vectorWordsList.size() + mappedWordsList.size()); // make sure we only have a single memory allocation
vectorWordsList.insert(vectorWordsList.end(), mappedWordsList.begin(), mappedWordsList.end());
Just use std::vector
's assign
member function.
//no need to call reserve, bidirectional iterators or better will compute the size and reserve internally.
vectorWordsList.assign(mappedWordsList.begin(), mappedWordsList.end());
If you have existing values in the vector that you don't want overwritten then use insert
instead like
vectorWordsList.reserve(vectorWordsList.size() + mappedWordsList.size()); // make sure we only have a single memory allocation
vectorWordsList.insert(vectorWordsList.end(), mappedWordsList.begin(), mappedWordsList.end());
edited Dec 20 '18 at 13:39
answered Dec 19 '18 at 15:38
NathanOliver
86.8k15120180
86.8k15120180
1
I would hope thatassign
would essentiallyreserve
internally.
– François Andrieux
Dec 19 '18 at 15:41
10
@FrançoisAndrieux It most likely would if you passed random access iterators but sincemap
has bidirectional iterators it requires a traversal to known how many elements to allocate space for so it isn't going to do that.
– NathanOliver
Dec 19 '18 at 15:43
1
@NathanOliver libstdc++ actually does essentiallyreserve
internally. For any ForwardIterator, it will callstd::distance
, paying for a traversal for anything less than a RandomAccessIterator. You can tracestd::vector::assign
to here.
– Justin
Dec 19 '18 at 19:43
1
@Justin Cool. Thanks for finding that. I still like to be explicit, in case other implementations don't do this.
– NathanOliver
Dec 19 '18 at 19:47
Putting a.resize(0)
before.reserve()
might be an optimization if the elements should be replaced.
– Deduplicator
Dec 19 '18 at 20:46
|
show 2 more comments
1
I would hope thatassign
would essentiallyreserve
internally.
– François Andrieux
Dec 19 '18 at 15:41
10
@FrançoisAndrieux It most likely would if you passed random access iterators but sincemap
has bidirectional iterators it requires a traversal to known how many elements to allocate space for so it isn't going to do that.
– NathanOliver
Dec 19 '18 at 15:43
1
@NathanOliver libstdc++ actually does essentiallyreserve
internally. For any ForwardIterator, it will callstd::distance
, paying for a traversal for anything less than a RandomAccessIterator. You can tracestd::vector::assign
to here.
– Justin
Dec 19 '18 at 19:43
1
@Justin Cool. Thanks for finding that. I still like to be explicit, in case other implementations don't do this.
– NathanOliver
Dec 19 '18 at 19:47
Putting a.resize(0)
before.reserve()
might be an optimization if the elements should be replaced.
– Deduplicator
Dec 19 '18 at 20:46
1
1
I would hope that
assign
would essentially reserve
internally.– François Andrieux
Dec 19 '18 at 15:41
I would hope that
assign
would essentially reserve
internally.– François Andrieux
Dec 19 '18 at 15:41
10
10
@FrançoisAndrieux It most likely would if you passed random access iterators but since
map
has bidirectional iterators it requires a traversal to known how many elements to allocate space for so it isn't going to do that.– NathanOliver
Dec 19 '18 at 15:43
@FrançoisAndrieux It most likely would if you passed random access iterators but since
map
has bidirectional iterators it requires a traversal to known how many elements to allocate space for so it isn't going to do that.– NathanOliver
Dec 19 '18 at 15:43
1
1
@NathanOliver libstdc++ actually does essentially
reserve
internally. For any ForwardIterator, it will call std::distance
, paying for a traversal for anything less than a RandomAccessIterator. You can trace std::vector::assign
to here.– Justin
Dec 19 '18 at 19:43
@NathanOliver libstdc++ actually does essentially
reserve
internally. For any ForwardIterator, it will call std::distance
, paying for a traversal for anything less than a RandomAccessIterator. You can trace std::vector::assign
to here.– Justin
Dec 19 '18 at 19:43
1
1
@Justin Cool. Thanks for finding that. I still like to be explicit, in case other implementations don't do this.
– NathanOliver
Dec 19 '18 at 19:47
@Justin Cool. Thanks for finding that. I still like to be explicit, in case other implementations don't do this.
– NathanOliver
Dec 19 '18 at 19:47
Putting a
.resize(0)
before .reserve()
might be an optimization if the elements should be replaced.– Deduplicator
Dec 19 '18 at 20:46
Putting a
.resize(0)
before .reserve()
might be an optimization if the elements should be replaced.– Deduplicator
Dec 19 '18 at 20:46
|
show 2 more comments
You can use std::copy
and std::back_inserter
:
std::copy(mappedWordsList.begin(),
mappedWordsList.end(),
std::back_inserter(vectorWordsList));
Honestly, I think that a range-for
loop is clearer:
for(const auto& kv : mappedWordsList)
vectorWordsList.emplace_back(kv);
Regardless, you can use std::vector::reserve
to preallocate memory on your target vector
, avoiding unnecessary reallocations.
add a comment |
You can use std::copy
and std::back_inserter
:
std::copy(mappedWordsList.begin(),
mappedWordsList.end(),
std::back_inserter(vectorWordsList));
Honestly, I think that a range-for
loop is clearer:
for(const auto& kv : mappedWordsList)
vectorWordsList.emplace_back(kv);
Regardless, you can use std::vector::reserve
to preallocate memory on your target vector
, avoiding unnecessary reallocations.
add a comment |
You can use std::copy
and std::back_inserter
:
std::copy(mappedWordsList.begin(),
mappedWordsList.end(),
std::back_inserter(vectorWordsList));
Honestly, I think that a range-for
loop is clearer:
for(const auto& kv : mappedWordsList)
vectorWordsList.emplace_back(kv);
Regardless, you can use std::vector::reserve
to preallocate memory on your target vector
, avoiding unnecessary reallocations.
You can use std::copy
and std::back_inserter
:
std::copy(mappedWordsList.begin(),
mappedWordsList.end(),
std::back_inserter(vectorWordsList));
Honestly, I think that a range-for
loop is clearer:
for(const auto& kv : mappedWordsList)
vectorWordsList.emplace_back(kv);
Regardless, you can use std::vector::reserve
to preallocate memory on your target vector
, avoiding unnecessary reallocations.
edited Dec 19 '18 at 15:36
answered Dec 19 '18 at 15:35
Vittorio Romeo
57.3k17154293
57.3k17154293
add a comment |
add a comment |
It's worth noting that if you are creating a vector for this purpose, you may use the vector's constructor directly:
std::vector<std::pair<FirstType,SecondType>> vectorWordsList( mappedWordsList.begin(), mappedWordsList.end() );
In C++17, you may also omit the vector's template arguments to have the compiler deduce them:
std::vector vectorWordsList( mappedWordsList.begin(), mappedWordsList.end() );
Unless you want to create a vector of iterators, you need parens.
– T.C.
Dec 20 '18 at 12:00
@T.C. thank you!
– Drew Dormann
Dec 20 '18 at 16:18
add a comment |
It's worth noting that if you are creating a vector for this purpose, you may use the vector's constructor directly:
std::vector<std::pair<FirstType,SecondType>> vectorWordsList( mappedWordsList.begin(), mappedWordsList.end() );
In C++17, you may also omit the vector's template arguments to have the compiler deduce them:
std::vector vectorWordsList( mappedWordsList.begin(), mappedWordsList.end() );
Unless you want to create a vector of iterators, you need parens.
– T.C.
Dec 20 '18 at 12:00
@T.C. thank you!
– Drew Dormann
Dec 20 '18 at 16:18
add a comment |
It's worth noting that if you are creating a vector for this purpose, you may use the vector's constructor directly:
std::vector<std::pair<FirstType,SecondType>> vectorWordsList( mappedWordsList.begin(), mappedWordsList.end() );
In C++17, you may also omit the vector's template arguments to have the compiler deduce them:
std::vector vectorWordsList( mappedWordsList.begin(), mappedWordsList.end() );
It's worth noting that if you are creating a vector for this purpose, you may use the vector's constructor directly:
std::vector<std::pair<FirstType,SecondType>> vectorWordsList( mappedWordsList.begin(), mappedWordsList.end() );
In C++17, you may also omit the vector's template arguments to have the compiler deduce them:
std::vector vectorWordsList( mappedWordsList.begin(), mappedWordsList.end() );
edited Dec 20 '18 at 16:19
answered Dec 19 '18 at 21:18
Drew Dormann
41.5k977140
41.5k977140
Unless you want to create a vector of iterators, you need parens.
– T.C.
Dec 20 '18 at 12:00
@T.C. thank you!
– Drew Dormann
Dec 20 '18 at 16:18
add a comment |
Unless you want to create a vector of iterators, you need parens.
– T.C.
Dec 20 '18 at 12:00
@T.C. thank you!
– Drew Dormann
Dec 20 '18 at 16:18
Unless you want to create a vector of iterators, you need parens.
– T.C.
Dec 20 '18 at 12:00
Unless you want to create a vector of iterators, you need parens.
– T.C.
Dec 20 '18 at 12:00
@T.C. thank you!
– Drew Dormann
Dec 20 '18 at 16:18
@T.C. thank you!
– Drew Dormann
Dec 20 '18 at 16:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53854468%2fcopy-stdmap-into-stdvector-of-pairs%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
@Lorand - The sort happens on the
second
item in the pair, which is not the key.– StoryTeller
Dec 19 '18 at 15:37
1
@Lorand It seems that OP wants to perform the sort based on the values, not the keys.
– ネロク
Dec 19 '18 at 15:38
3
Seems like that question should be flagged as a duplicate of this one, since the answer provided by @NathanOliver is way better than any of the answers to that question. Edit : Though that question includes sorting the result.
– François Andrieux
Dec 19 '18 at 15:40
1
@ALX23z - I agree that
[=]
should not be present. But just because it's there doesn't mean everything gets copied. The lambda has to use something from the surrounding scope to capture it.– StoryTeller
Dec 19 '18 at 15:46
1
@ALX23z Actually, it will only copy a variable is you use it in the lambda body. There is no performance penalty to use
[=]
if you don't use any of the variables in scope.– NathanOliver
Dec 19 '18 at 15:46