How do I compare the first element (string) of a pair in a pair vector with another string?











up vote
-2
down vote

favorite












I'm trying to implement what I've learned about std::vector to solve a problem. User is going to input a list of animal names (not specified how many) and I need to record the animal names and their occurrences in the user input. At first, I tried using an array, but as it's static, I turned to std::vector. Again, at first, I tried using two std::vector, one with type int and the other with type string to store the animal names and the amount of occurrences. However, it seems a bit hard to sort both vectors later and a std::vector with a pair type sounds better in my mind. And now, I'm stuck with this code below with errors I don't quite understand:



#include <bits/stdc++.h>
#include <sstream>

using namespace std;

int position(vector< pair<string, int> > myList, string animalName) {
int pos;
for (int i = 0; i < myList.size(); i++) if (animalName.compare(myList[i].first) == 0) pos = i;
return pos;
}

int main() {
int Q;
cin >> Q;

vector< pair<string, int> > zooPair;
string animal;

for (int i = 0; i < Q; i++){
cin >> animal;
if (find_if(zooPair.begin(), zooPair.end(), animal.compare(zooPair.first) == 0) == zooPair.end())
zooPair.emplace_back(animal, 1);
else
zooPair[position(zooPair, animal)].second += 1;
}

sort(zooPair.begin(), zooPair.end());

for (vector< pair<string, int> >::iterator it = zooList.begin(); it != zooList.end(); it++)
cout << *it;

return 0;
}









share|improve this question






















  • You have to give a comparator to sort
    – gsamaras
    Nov 10 at 9:47










  • @gsamaras What does 'comparator' mean?
    – Wealthy Player
    Nov 10 at 9:56










  • It tells sort how to compare the elements of the vector, so that it can order them.
    – gsamaras
    Nov 10 at 9:58










  • @gsamaras cplusplus.com/reference/algorithm/sort I see! So I need to place zooPair.first as a comparator? It will automatically sort it in an ascending order, right?
    – Wealthy Player
    Nov 10 at 10:00










  • Wealthy I would just provide a comparator who would compare the first field, so I think we say the same. That's different than the accepted answer now from Klaus, but since you are happy with it, then I am happy. :)
    – gsamaras
    Nov 10 at 11:44















up vote
-2
down vote

favorite












I'm trying to implement what I've learned about std::vector to solve a problem. User is going to input a list of animal names (not specified how many) and I need to record the animal names and their occurrences in the user input. At first, I tried using an array, but as it's static, I turned to std::vector. Again, at first, I tried using two std::vector, one with type int and the other with type string to store the animal names and the amount of occurrences. However, it seems a bit hard to sort both vectors later and a std::vector with a pair type sounds better in my mind. And now, I'm stuck with this code below with errors I don't quite understand:



#include <bits/stdc++.h>
#include <sstream>

using namespace std;

int position(vector< pair<string, int> > myList, string animalName) {
int pos;
for (int i = 0; i < myList.size(); i++) if (animalName.compare(myList[i].first) == 0) pos = i;
return pos;
}

int main() {
int Q;
cin >> Q;

vector< pair<string, int> > zooPair;
string animal;

for (int i = 0; i < Q; i++){
cin >> animal;
if (find_if(zooPair.begin(), zooPair.end(), animal.compare(zooPair.first) == 0) == zooPair.end())
zooPair.emplace_back(animal, 1);
else
zooPair[position(zooPair, animal)].second += 1;
}

sort(zooPair.begin(), zooPair.end());

for (vector< pair<string, int> >::iterator it = zooList.begin(); it != zooList.end(); it++)
cout << *it;

return 0;
}









share|improve this question






















  • You have to give a comparator to sort
    – gsamaras
    Nov 10 at 9:47










  • @gsamaras What does 'comparator' mean?
    – Wealthy Player
    Nov 10 at 9:56










  • It tells sort how to compare the elements of the vector, so that it can order them.
    – gsamaras
    Nov 10 at 9:58










  • @gsamaras cplusplus.com/reference/algorithm/sort I see! So I need to place zooPair.first as a comparator? It will automatically sort it in an ascending order, right?
    – Wealthy Player
    Nov 10 at 10:00










  • Wealthy I would just provide a comparator who would compare the first field, so I think we say the same. That's different than the accepted answer now from Klaus, but since you are happy with it, then I am happy. :)
    – gsamaras
    Nov 10 at 11:44













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I'm trying to implement what I've learned about std::vector to solve a problem. User is going to input a list of animal names (not specified how many) and I need to record the animal names and their occurrences in the user input. At first, I tried using an array, but as it's static, I turned to std::vector. Again, at first, I tried using two std::vector, one with type int and the other with type string to store the animal names and the amount of occurrences. However, it seems a bit hard to sort both vectors later and a std::vector with a pair type sounds better in my mind. And now, I'm stuck with this code below with errors I don't quite understand:



#include <bits/stdc++.h>
#include <sstream>

using namespace std;

int position(vector< pair<string, int> > myList, string animalName) {
int pos;
for (int i = 0; i < myList.size(); i++) if (animalName.compare(myList[i].first) == 0) pos = i;
return pos;
}

int main() {
int Q;
cin >> Q;

vector< pair<string, int> > zooPair;
string animal;

for (int i = 0; i < Q; i++){
cin >> animal;
if (find_if(zooPair.begin(), zooPair.end(), animal.compare(zooPair.first) == 0) == zooPair.end())
zooPair.emplace_back(animal, 1);
else
zooPair[position(zooPair, animal)].second += 1;
}

sort(zooPair.begin(), zooPair.end());

for (vector< pair<string, int> >::iterator it = zooList.begin(); it != zooList.end(); it++)
cout << *it;

return 0;
}









share|improve this question













I'm trying to implement what I've learned about std::vector to solve a problem. User is going to input a list of animal names (not specified how many) and I need to record the animal names and their occurrences in the user input. At first, I tried using an array, but as it's static, I turned to std::vector. Again, at first, I tried using two std::vector, one with type int and the other with type string to store the animal names and the amount of occurrences. However, it seems a bit hard to sort both vectors later and a std::vector with a pair type sounds better in my mind. And now, I'm stuck with this code below with errors I don't quite understand:



#include <bits/stdc++.h>
#include <sstream>

using namespace std;

int position(vector< pair<string, int> > myList, string animalName) {
int pos;
for (int i = 0; i < myList.size(); i++) if (animalName.compare(myList[i].first) == 0) pos = i;
return pos;
}

int main() {
int Q;
cin >> Q;

vector< pair<string, int> > zooPair;
string animal;

for (int i = 0; i < Q; i++){
cin >> animal;
if (find_if(zooPair.begin(), zooPair.end(), animal.compare(zooPair.first) == 0) == zooPair.end())
zooPair.emplace_back(animal, 1);
else
zooPair[position(zooPair, animal)].second += 1;
}

sort(zooPair.begin(), zooPair.end());

for (vector< pair<string, int> >::iterator it = zooList.begin(); it != zooList.end(); it++)
cout << *it;

return 0;
}






c++ vector






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 9:44









Wealthy Player

1118




1118












  • You have to give a comparator to sort
    – gsamaras
    Nov 10 at 9:47










  • @gsamaras What does 'comparator' mean?
    – Wealthy Player
    Nov 10 at 9:56










  • It tells sort how to compare the elements of the vector, so that it can order them.
    – gsamaras
    Nov 10 at 9:58










  • @gsamaras cplusplus.com/reference/algorithm/sort I see! So I need to place zooPair.first as a comparator? It will automatically sort it in an ascending order, right?
    – Wealthy Player
    Nov 10 at 10:00










  • Wealthy I would just provide a comparator who would compare the first field, so I think we say the same. That's different than the accepted answer now from Klaus, but since you are happy with it, then I am happy. :)
    – gsamaras
    Nov 10 at 11:44


















  • You have to give a comparator to sort
    – gsamaras
    Nov 10 at 9:47










  • @gsamaras What does 'comparator' mean?
    – Wealthy Player
    Nov 10 at 9:56










  • It tells sort how to compare the elements of the vector, so that it can order them.
    – gsamaras
    Nov 10 at 9:58










  • @gsamaras cplusplus.com/reference/algorithm/sort I see! So I need to place zooPair.first as a comparator? It will automatically sort it in an ascending order, right?
    – Wealthy Player
    Nov 10 at 10:00










  • Wealthy I would just provide a comparator who would compare the first field, so I think we say the same. That's different than the accepted answer now from Klaus, but since you are happy with it, then I am happy. :)
    – gsamaras
    Nov 10 at 11:44
















You have to give a comparator to sort
– gsamaras
Nov 10 at 9:47




You have to give a comparator to sort
– gsamaras
Nov 10 at 9:47












@gsamaras What does 'comparator' mean?
– Wealthy Player
Nov 10 at 9:56




@gsamaras What does 'comparator' mean?
– Wealthy Player
Nov 10 at 9:56












It tells sort how to compare the elements of the vector, so that it can order them.
– gsamaras
Nov 10 at 9:58




It tells sort how to compare the elements of the vector, so that it can order them.
– gsamaras
Nov 10 at 9:58












@gsamaras cplusplus.com/reference/algorithm/sort I see! So I need to place zooPair.first as a comparator? It will automatically sort it in an ascending order, right?
– Wealthy Player
Nov 10 at 10:00




@gsamaras cplusplus.com/reference/algorithm/sort I see! So I need to place zooPair.first as a comparator? It will automatically sort it in an ascending order, right?
– Wealthy Player
Nov 10 at 10:00












Wealthy I would just provide a comparator who would compare the first field, so I think we say the same. That's different than the accepted answer now from Klaus, but since you are happy with it, then I am happy. :)
– gsamaras
Nov 10 at 11:44




Wealthy I would just provide a comparator who would compare the first field, so I think we say the same. That's different than the accepted answer now from Klaus, but since you are happy with it, then I am happy. :)
– gsamaras
Nov 10 at 11:44












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










You simply should use std::map as the container type, because it already sorts, have a easy to use access interface with operator. Here we create a std::map with your animal and the number of them in your zoo.



Example:



int main() {
int Q;
std::cout << "Enter number of entries" << std::endl;
std::cin >> Q;

std::map<std::string, int> zoo;

std::string animal;

for (int i = 0; i < Q; i++){
std::cout << "Enter animal" << std::endl;
std::cin >> animal;
zoo[animal]++;
}

for ( auto& it: zoo )
{
std::cout << it.first << " " << it.second << std::endl;
}


return 0;
}


As you see, there is no need for an additional sort as a map is always sorted for the first part, named the "key", of every entry.



The same with std::vector. Remark that you have to give operators for sort and find!



Full example:



struct SortableElements: public std::pair< std::string, int >
{
// forward construction
using std::pair<std::string, int>::pair;

// use for sort:
bool operator < (const SortableElements& e2 ) const
{
return first < e2.first;
}

// use for find:
bool operator == ( const std::string& e2 ) const
{
return first == e2;
}
};



int main()
{
std::vector< SortableElements > zoo;

int Q;
std::cout << "Enter number of entries" << std::endl;
std::cin >> Q;

std::string animal;

for (int i = 0; i < Q; i++){
std::cout << "Enter animal" << std::endl;
std::cin >> animal;

auto it = std::find( zoo.begin(), zoo.end(), animal);
if ( it != zoo.end())
{
it->second++;
}
else
{
zoo.emplace_back( animal, 1 );
}
}

// sort:
std::sort(zoo.begin(), zoo.end());

for ( auto& it: zoo )
{
std::cout << it.first << " " << it.second << std::endl;
}

return 0;
}





share|improve this answer























  • What do zoo[animal]++ and auto& it: zoo mean?
    – Wealthy Player
    Nov 10 at 10:08










  • @WealthyPlayer: Please read the documentation for std::map. cplusplus.com/reference/map/map/operator%5B%5D And for the loop: en.cppreference.com/w/cpp/language/range-for
    – Klaus
    Nov 10 at 10:13












  • I see, thank you! Does it automatically initialise the second element (the int) of the entries as 0 if the key I insert creates a new entry? Also, what does auto& it: zoo mean? After reading some other posts, it seems like auto& automatically infers the type of the item next to it. But what does the : zoo do then?
    – Wealthy Player
    Nov 10 at 10:16












  • @WealthyPlayer: Sorry, did you read about "range based for". The link is already there!
    – Klaus
    Nov 10 at 10:26










  • Sorry, didn't notice the edit, but I've read your link and found it quite confusing. So I tried this link too (as you've given me the name): geeksforgeeks.org/range-based-loop-c. I understand what auto does now and the other non-forwarding accesses now, but what does the & mean in your code? It seems like there's auto&& , though, in the link you provided. However, I don't quite understand what 'forwarding reference' meant. Should I not be bothered by that? What if I don't use & in your code above? Can I achieve the same results with access by value?
    – Wealthy Player
    Nov 10 at 10:33













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',
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237733%2fhow-do-i-compare-the-first-element-string-of-a-pair-in-a-pair-vector-with-anot%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










You simply should use std::map as the container type, because it already sorts, have a easy to use access interface with operator. Here we create a std::map with your animal and the number of them in your zoo.



Example:



int main() {
int Q;
std::cout << "Enter number of entries" << std::endl;
std::cin >> Q;

std::map<std::string, int> zoo;

std::string animal;

for (int i = 0; i < Q; i++){
std::cout << "Enter animal" << std::endl;
std::cin >> animal;
zoo[animal]++;
}

for ( auto& it: zoo )
{
std::cout << it.first << " " << it.second << std::endl;
}


return 0;
}


As you see, there is no need for an additional sort as a map is always sorted for the first part, named the "key", of every entry.



The same with std::vector. Remark that you have to give operators for sort and find!



Full example:



struct SortableElements: public std::pair< std::string, int >
{
// forward construction
using std::pair<std::string, int>::pair;

// use for sort:
bool operator < (const SortableElements& e2 ) const
{
return first < e2.first;
}

// use for find:
bool operator == ( const std::string& e2 ) const
{
return first == e2;
}
};



int main()
{
std::vector< SortableElements > zoo;

int Q;
std::cout << "Enter number of entries" << std::endl;
std::cin >> Q;

std::string animal;

for (int i = 0; i < Q; i++){
std::cout << "Enter animal" << std::endl;
std::cin >> animal;

auto it = std::find( zoo.begin(), zoo.end(), animal);
if ( it != zoo.end())
{
it->second++;
}
else
{
zoo.emplace_back( animal, 1 );
}
}

// sort:
std::sort(zoo.begin(), zoo.end());

for ( auto& it: zoo )
{
std::cout << it.first << " " << it.second << std::endl;
}

return 0;
}





share|improve this answer























  • What do zoo[animal]++ and auto& it: zoo mean?
    – Wealthy Player
    Nov 10 at 10:08










  • @WealthyPlayer: Please read the documentation for std::map. cplusplus.com/reference/map/map/operator%5B%5D And for the loop: en.cppreference.com/w/cpp/language/range-for
    – Klaus
    Nov 10 at 10:13












  • I see, thank you! Does it automatically initialise the second element (the int) of the entries as 0 if the key I insert creates a new entry? Also, what does auto& it: zoo mean? After reading some other posts, it seems like auto& automatically infers the type of the item next to it. But what does the : zoo do then?
    – Wealthy Player
    Nov 10 at 10:16












  • @WealthyPlayer: Sorry, did you read about "range based for". The link is already there!
    – Klaus
    Nov 10 at 10:26










  • Sorry, didn't notice the edit, but I've read your link and found it quite confusing. So I tried this link too (as you've given me the name): geeksforgeeks.org/range-based-loop-c. I understand what auto does now and the other non-forwarding accesses now, but what does the & mean in your code? It seems like there's auto&& , though, in the link you provided. However, I don't quite understand what 'forwarding reference' meant. Should I not be bothered by that? What if I don't use & in your code above? Can I achieve the same results with access by value?
    – Wealthy Player
    Nov 10 at 10:33

















up vote
1
down vote



accepted










You simply should use std::map as the container type, because it already sorts, have a easy to use access interface with operator. Here we create a std::map with your animal and the number of them in your zoo.



Example:



int main() {
int Q;
std::cout << "Enter number of entries" << std::endl;
std::cin >> Q;

std::map<std::string, int> zoo;

std::string animal;

for (int i = 0; i < Q; i++){
std::cout << "Enter animal" << std::endl;
std::cin >> animal;
zoo[animal]++;
}

for ( auto& it: zoo )
{
std::cout << it.first << " " << it.second << std::endl;
}


return 0;
}


As you see, there is no need for an additional sort as a map is always sorted for the first part, named the "key", of every entry.



The same with std::vector. Remark that you have to give operators for sort and find!



Full example:



struct SortableElements: public std::pair< std::string, int >
{
// forward construction
using std::pair<std::string, int>::pair;

// use for sort:
bool operator < (const SortableElements& e2 ) const
{
return first < e2.first;
}

// use for find:
bool operator == ( const std::string& e2 ) const
{
return first == e2;
}
};



int main()
{
std::vector< SortableElements > zoo;

int Q;
std::cout << "Enter number of entries" << std::endl;
std::cin >> Q;

std::string animal;

for (int i = 0; i < Q; i++){
std::cout << "Enter animal" << std::endl;
std::cin >> animal;

auto it = std::find( zoo.begin(), zoo.end(), animal);
if ( it != zoo.end())
{
it->second++;
}
else
{
zoo.emplace_back( animal, 1 );
}
}

// sort:
std::sort(zoo.begin(), zoo.end());

for ( auto& it: zoo )
{
std::cout << it.first << " " << it.second << std::endl;
}

return 0;
}





share|improve this answer























  • What do zoo[animal]++ and auto& it: zoo mean?
    – Wealthy Player
    Nov 10 at 10:08










  • @WealthyPlayer: Please read the documentation for std::map. cplusplus.com/reference/map/map/operator%5B%5D And for the loop: en.cppreference.com/w/cpp/language/range-for
    – Klaus
    Nov 10 at 10:13












  • I see, thank you! Does it automatically initialise the second element (the int) of the entries as 0 if the key I insert creates a new entry? Also, what does auto& it: zoo mean? After reading some other posts, it seems like auto& automatically infers the type of the item next to it. But what does the : zoo do then?
    – Wealthy Player
    Nov 10 at 10:16












  • @WealthyPlayer: Sorry, did you read about "range based for". The link is already there!
    – Klaus
    Nov 10 at 10:26










  • Sorry, didn't notice the edit, but I've read your link and found it quite confusing. So I tried this link too (as you've given me the name): geeksforgeeks.org/range-based-loop-c. I understand what auto does now and the other non-forwarding accesses now, but what does the & mean in your code? It seems like there's auto&& , though, in the link you provided. However, I don't quite understand what 'forwarding reference' meant. Should I not be bothered by that? What if I don't use & in your code above? Can I achieve the same results with access by value?
    – Wealthy Player
    Nov 10 at 10:33















up vote
1
down vote



accepted







up vote
1
down vote



accepted






You simply should use std::map as the container type, because it already sorts, have a easy to use access interface with operator. Here we create a std::map with your animal and the number of them in your zoo.



Example:



int main() {
int Q;
std::cout << "Enter number of entries" << std::endl;
std::cin >> Q;

std::map<std::string, int> zoo;

std::string animal;

for (int i = 0; i < Q; i++){
std::cout << "Enter animal" << std::endl;
std::cin >> animal;
zoo[animal]++;
}

for ( auto& it: zoo )
{
std::cout << it.first << " " << it.second << std::endl;
}


return 0;
}


As you see, there is no need for an additional sort as a map is always sorted for the first part, named the "key", of every entry.



The same with std::vector. Remark that you have to give operators for sort and find!



Full example:



struct SortableElements: public std::pair< std::string, int >
{
// forward construction
using std::pair<std::string, int>::pair;

// use for sort:
bool operator < (const SortableElements& e2 ) const
{
return first < e2.first;
}

// use for find:
bool operator == ( const std::string& e2 ) const
{
return first == e2;
}
};



int main()
{
std::vector< SortableElements > zoo;

int Q;
std::cout << "Enter number of entries" << std::endl;
std::cin >> Q;

std::string animal;

for (int i = 0; i < Q; i++){
std::cout << "Enter animal" << std::endl;
std::cin >> animal;

auto it = std::find( zoo.begin(), zoo.end(), animal);
if ( it != zoo.end())
{
it->second++;
}
else
{
zoo.emplace_back( animal, 1 );
}
}

// sort:
std::sort(zoo.begin(), zoo.end());

for ( auto& it: zoo )
{
std::cout << it.first << " " << it.second << std::endl;
}

return 0;
}





share|improve this answer














You simply should use std::map as the container type, because it already sorts, have a easy to use access interface with operator. Here we create a std::map with your animal and the number of them in your zoo.



Example:



int main() {
int Q;
std::cout << "Enter number of entries" << std::endl;
std::cin >> Q;

std::map<std::string, int> zoo;

std::string animal;

for (int i = 0; i < Q; i++){
std::cout << "Enter animal" << std::endl;
std::cin >> animal;
zoo[animal]++;
}

for ( auto& it: zoo )
{
std::cout << it.first << " " << it.second << std::endl;
}


return 0;
}


As you see, there is no need for an additional sort as a map is always sorted for the first part, named the "key", of every entry.



The same with std::vector. Remark that you have to give operators for sort and find!



Full example:



struct SortableElements: public std::pair< std::string, int >
{
// forward construction
using std::pair<std::string, int>::pair;

// use for sort:
bool operator < (const SortableElements& e2 ) const
{
return first < e2.first;
}

// use for find:
bool operator == ( const std::string& e2 ) const
{
return first == e2;
}
};



int main()
{
std::vector< SortableElements > zoo;

int Q;
std::cout << "Enter number of entries" << std::endl;
std::cin >> Q;

std::string animal;

for (int i = 0; i < Q; i++){
std::cout << "Enter animal" << std::endl;
std::cin >> animal;

auto it = std::find( zoo.begin(), zoo.end(), animal);
if ( it != zoo.end())
{
it->second++;
}
else
{
zoo.emplace_back( animal, 1 );
}
}

// sort:
std::sort(zoo.begin(), zoo.end());

for ( auto& it: zoo )
{
std::cout << it.first << " " << it.second << std::endl;
}

return 0;
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 11:23

























answered Nov 10 at 10:02









Klaus

10.7k12558




10.7k12558












  • What do zoo[animal]++ and auto& it: zoo mean?
    – Wealthy Player
    Nov 10 at 10:08










  • @WealthyPlayer: Please read the documentation for std::map. cplusplus.com/reference/map/map/operator%5B%5D And for the loop: en.cppreference.com/w/cpp/language/range-for
    – Klaus
    Nov 10 at 10:13












  • I see, thank you! Does it automatically initialise the second element (the int) of the entries as 0 if the key I insert creates a new entry? Also, what does auto& it: zoo mean? After reading some other posts, it seems like auto& automatically infers the type of the item next to it. But what does the : zoo do then?
    – Wealthy Player
    Nov 10 at 10:16












  • @WealthyPlayer: Sorry, did you read about "range based for". The link is already there!
    – Klaus
    Nov 10 at 10:26










  • Sorry, didn't notice the edit, but I've read your link and found it quite confusing. So I tried this link too (as you've given me the name): geeksforgeeks.org/range-based-loop-c. I understand what auto does now and the other non-forwarding accesses now, but what does the & mean in your code? It seems like there's auto&& , though, in the link you provided. However, I don't quite understand what 'forwarding reference' meant. Should I not be bothered by that? What if I don't use & in your code above? Can I achieve the same results with access by value?
    – Wealthy Player
    Nov 10 at 10:33




















  • What do zoo[animal]++ and auto& it: zoo mean?
    – Wealthy Player
    Nov 10 at 10:08










  • @WealthyPlayer: Please read the documentation for std::map. cplusplus.com/reference/map/map/operator%5B%5D And for the loop: en.cppreference.com/w/cpp/language/range-for
    – Klaus
    Nov 10 at 10:13












  • I see, thank you! Does it automatically initialise the second element (the int) of the entries as 0 if the key I insert creates a new entry? Also, what does auto& it: zoo mean? After reading some other posts, it seems like auto& automatically infers the type of the item next to it. But what does the : zoo do then?
    – Wealthy Player
    Nov 10 at 10:16












  • @WealthyPlayer: Sorry, did you read about "range based for". The link is already there!
    – Klaus
    Nov 10 at 10:26










  • Sorry, didn't notice the edit, but I've read your link and found it quite confusing. So I tried this link too (as you've given me the name): geeksforgeeks.org/range-based-loop-c. I understand what auto does now and the other non-forwarding accesses now, but what does the & mean in your code? It seems like there's auto&& , though, in the link you provided. However, I don't quite understand what 'forwarding reference' meant. Should I not be bothered by that? What if I don't use & in your code above? Can I achieve the same results with access by value?
    – Wealthy Player
    Nov 10 at 10:33


















What do zoo[animal]++ and auto& it: zoo mean?
– Wealthy Player
Nov 10 at 10:08




What do zoo[animal]++ and auto& it: zoo mean?
– Wealthy Player
Nov 10 at 10:08












@WealthyPlayer: Please read the documentation for std::map. cplusplus.com/reference/map/map/operator%5B%5D And for the loop: en.cppreference.com/w/cpp/language/range-for
– Klaus
Nov 10 at 10:13






@WealthyPlayer: Please read the documentation for std::map. cplusplus.com/reference/map/map/operator%5B%5D And for the loop: en.cppreference.com/w/cpp/language/range-for
– Klaus
Nov 10 at 10:13














I see, thank you! Does it automatically initialise the second element (the int) of the entries as 0 if the key I insert creates a new entry? Also, what does auto& it: zoo mean? After reading some other posts, it seems like auto& automatically infers the type of the item next to it. But what does the : zoo do then?
– Wealthy Player
Nov 10 at 10:16






I see, thank you! Does it automatically initialise the second element (the int) of the entries as 0 if the key I insert creates a new entry? Also, what does auto& it: zoo mean? After reading some other posts, it seems like auto& automatically infers the type of the item next to it. But what does the : zoo do then?
– Wealthy Player
Nov 10 at 10:16














@WealthyPlayer: Sorry, did you read about "range based for". The link is already there!
– Klaus
Nov 10 at 10:26




@WealthyPlayer: Sorry, did you read about "range based for". The link is already there!
– Klaus
Nov 10 at 10:26












Sorry, didn't notice the edit, but I've read your link and found it quite confusing. So I tried this link too (as you've given me the name): geeksforgeeks.org/range-based-loop-c. I understand what auto does now and the other non-forwarding accesses now, but what does the & mean in your code? It seems like there's auto&& , though, in the link you provided. However, I don't quite understand what 'forwarding reference' meant. Should I not be bothered by that? What if I don't use & in your code above? Can I achieve the same results with access by value?
– Wealthy Player
Nov 10 at 10:33






Sorry, didn't notice the edit, but I've read your link and found it quite confusing. So I tried this link too (as you've given me the name): geeksforgeeks.org/range-based-loop-c. I understand what auto does now and the other non-forwarding accesses now, but what does the & mean in your code? It seems like there's auto&& , though, in the link you provided. However, I don't quite understand what 'forwarding reference' meant. Should I not be bothered by that? What if I don't use & in your code above? Can I achieve the same results with access by value?
– Wealthy Player
Nov 10 at 10:33




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237733%2fhow-do-i-compare-the-first-element-string-of-a-pair-in-a-pair-vector-with-anot%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff