This is the exercise I did:
Write a program to read a sequence of strings and ints,
storing each into a pair. Store the pairs in a vector.
Here is my code:
#include <utility>
#include <vector>
#include <iostream>
std::pair<std::string, int>readin(std::string s, int i);
int main()
{
std::string s;
int i(0);
std::vector<std::pair<std::string, int>>store;
while(std::cin>>s>>i)
{
store.push_back(readin(s, i));
}
std::cout<<store[0].first <<'\n';
std::cout <<store[0].second<<'\n';
}
std::pair<std::string, int>readin(std::string s, int i)
{
std::pair<std::string, int>couple={s, i};
return couple;
}
The output seems ok, but is there a better way going about this.
Just curious if any of the experts here could point out a simpler or more optimised solution for this problem.
THanks

New Topic/Question
Reply



MultiQuote



|