In C ++, we usually use std::stringthrough char[]. In the first case, the equality operator is overloaded, so your code will work. With the latter you need strcmp()to achieve your goal.
( std::vector, , ):
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
ifstream infile("data.txt");
int n;
infile >> n;
vector<string> name(n);
int amount[n], i = 0;
while (infile >> name[i] >> amount[i])
{
cout << name[i] << " " << amount[i] << endl;
i++;
}
return 0;
}
, std::pair, , .
, main() return 0;, , 1.
PS: :
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <utility>
using namespace std;
int main()
{
ifstream infile("data.txt");
int n;
infile >> n;
vector<string> name(n);
int amount[n], i = 0;
while (infile >> name[i] >> amount[i])
{
i++;
}
vector< pair<string, int> > result;
bool found;
for(int i = 0; i < name.size(); ++i)
{
found = false;
for(int j = 0; j < result.size(); ++j)
{
if(name[i] == result[j].first)
{
result[j].second += amount[i];
found = true;
}
}
if(!found)
{
result.push_back({name[i], amount[i]});
}
}
cout << "RESULTS:\n";
for(int i = 0; i < result.size(); ++i)
cout << result[i].first << " " << result[i].second << endl;
return 0;
}
:
Georgioss-MacBook-Pro:~ gsamaras$ g++ -Wall -std=c++0x main.cpp
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out
RESULTS:
wood 19
gold 21
silver 15