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;
}
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.