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;
}
c++ vector
add a comment |
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;
}
c++ vector
You have to give a comparator to sort
– gsamaras
21 hours ago
@gsamaras What does 'comparator' mean?
– Wealthy Player
21 hours ago
It tells sort how to compare the elements of the vector, so that it can order them.
– gsamaras
21 hours ago
@gsamaras cplusplus.com/reference/algorithm/sort I see! So I need to placezooPair.first
as a comparator? It will automatically sort it in an ascending order, right?
– Wealthy Player
21 hours ago
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
19 hours ago
add a comment |
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;
}
c++ vector
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
c++ vector
asked 21 hours ago
Wealthy Player
1506
1506
You have to give a comparator to sort
– gsamaras
21 hours ago
@gsamaras What does 'comparator' mean?
– Wealthy Player
21 hours ago
It tells sort how to compare the elements of the vector, so that it can order them.
– gsamaras
21 hours ago
@gsamaras cplusplus.com/reference/algorithm/sort I see! So I need to placezooPair.first
as a comparator? It will automatically sort it in an ascending order, right?
– Wealthy Player
21 hours ago
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
19 hours ago
add a comment |
You have to give a comparator to sort
– gsamaras
21 hours ago
@gsamaras What does 'comparator' mean?
– Wealthy Player
21 hours ago
It tells sort how to compare the elements of the vector, so that it can order them.
– gsamaras
21 hours ago
@gsamaras cplusplus.com/reference/algorithm/sort I see! So I need to placezooPair.first
as a comparator? It will automatically sort it in an ascending order, right?
– Wealthy Player
21 hours ago
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
19 hours ago
You have to give a comparator to sort
– gsamaras
21 hours ago
You have to give a comparator to sort
– gsamaras
21 hours ago
@gsamaras What does 'comparator' mean?
– Wealthy Player
21 hours ago
@gsamaras What does 'comparator' mean?
– Wealthy Player
21 hours ago
It tells sort how to compare the elements of the vector, so that it can order them.
– gsamaras
21 hours ago
It tells sort how to compare the elements of the vector, so that it can order them.
– gsamaras
21 hours ago
@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
21 hours ago
@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
21 hours ago
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
19 hours ago
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
19 hours ago
add a comment |
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;
}
What dozoo[animal]++
andauto& it: zoo
mean?
– Wealthy Player
21 hours ago
@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
21 hours ago
I see, thank you! Does it automatically initialise the second element (theint
) of the entries as 0 if the key I insert creates a new entry? Also, what doesauto& it: zoo
mean? After reading some other posts, it seems likeauto&
automatically infers the type of the item next to it. But what does the: zoo
do then?
– Wealthy Player
20 hours ago
@WealthyPlayer: Sorry, did you read about "range based for". The link is already there!
– Klaus
20 hours ago
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 whatauto
does now and the other non-forwarding accesses now, but what does the&
mean in your code? It seems like there'sauto&&
, 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
20 hours ago
|
show 8 more comments
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;
}
What dozoo[animal]++
andauto& it: zoo
mean?
– Wealthy Player
21 hours ago
@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
21 hours ago
I see, thank you! Does it automatically initialise the second element (theint
) of the entries as 0 if the key I insert creates a new entry? Also, what doesauto& it: zoo
mean? After reading some other posts, it seems likeauto&
automatically infers the type of the item next to it. But what does the: zoo
do then?
– Wealthy Player
20 hours ago
@WealthyPlayer: Sorry, did you read about "range based for". The link is already there!
– Klaus
20 hours ago
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 whatauto
does now and the other non-forwarding accesses now, but what does the&
mean in your code? It seems like there'sauto&&
, 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
20 hours ago
|
show 8 more comments
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;
}
What dozoo[animal]++
andauto& it: zoo
mean?
– Wealthy Player
21 hours ago
@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
21 hours ago
I see, thank you! Does it automatically initialise the second element (theint
) of the entries as 0 if the key I insert creates a new entry? Also, what doesauto& it: zoo
mean? After reading some other posts, it seems likeauto&
automatically infers the type of the item next to it. But what does the: zoo
do then?
– Wealthy Player
20 hours ago
@WealthyPlayer: Sorry, did you read about "range based for". The link is already there!
– Klaus
20 hours ago
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 whatauto
does now and the other non-forwarding accesses now, but what does the&
mean in your code? It seems like there'sauto&&
, 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
20 hours ago
|
show 8 more comments
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;
}
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;
}
edited 19 hours ago
answered 21 hours ago
Klaus
10.2k12557
10.2k12557
What dozoo[animal]++
andauto& it: zoo
mean?
– Wealthy Player
21 hours ago
@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
21 hours ago
I see, thank you! Does it automatically initialise the second element (theint
) of the entries as 0 if the key I insert creates a new entry? Also, what doesauto& it: zoo
mean? After reading some other posts, it seems likeauto&
automatically infers the type of the item next to it. But what does the: zoo
do then?
– Wealthy Player
20 hours ago
@WealthyPlayer: Sorry, did you read about "range based for". The link is already there!
– Klaus
20 hours ago
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 whatauto
does now and the other non-forwarding accesses now, but what does the&
mean in your code? It seems like there'sauto&&
, 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
20 hours ago
|
show 8 more comments
What dozoo[animal]++
andauto& it: zoo
mean?
– Wealthy Player
21 hours ago
@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
21 hours ago
I see, thank you! Does it automatically initialise the second element (theint
) of the entries as 0 if the key I insert creates a new entry? Also, what doesauto& it: zoo
mean? After reading some other posts, it seems likeauto&
automatically infers the type of the item next to it. But what does the: zoo
do then?
– Wealthy Player
20 hours ago
@WealthyPlayer: Sorry, did you read about "range based for". The link is already there!
– Klaus
20 hours ago
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 whatauto
does now and the other non-forwarding accesses now, but what does the&
mean in your code? It seems like there'sauto&&
, 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
20 hours ago
What do
zoo[animal]++
and auto& it: zoo
mean?– Wealthy Player
21 hours ago
What do
zoo[animal]++
and auto& it: zoo
mean?– Wealthy Player
21 hours ago
@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
21 hours ago
@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
21 hours ago
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
20 hours ago
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
20 hours ago
@WealthyPlayer: Sorry, did you read about "range based for". The link is already there!
– Klaus
20 hours ago
@WealthyPlayer: Sorry, did you read about "range based for". The link is already there!
– Klaus
20 hours ago
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
20 hours ago
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
20 hours ago
|
show 8 more comments
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
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
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
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
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
You have to give a comparator to sort
– gsamaras
21 hours ago
@gsamaras What does 'comparator' mean?
– Wealthy Player
21 hours ago
It tells sort how to compare the elements of the vector, so that it can order them.
– gsamaras
21 hours ago
@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
21 hours ago
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
19 hours ago