Q:

Write a program to read a sequence of strings and ints, storing each into a pair. Store the pairs in a vector

0

Write a program to read a sequence of strings and ints, storing each into a pair. Store the pairs in a vector.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <string>
#include <utility>
#include <vector>
#include <iostream>

int main() {
  std::vector<std::pair<std::string, int>> vp;
  std::string s;
  int i;
  while (std::cin >> s >> i)
    vp.push_back({s, i});
    //vp.push_back(std::make_pair(s, i));
    //vp.push_back(std::pair<std::string, int>(s, i));
  // I think the braced inititalizer is the most easy way.

  for (const auto &p : vp)
    std::cout << p.first << " : " << p.second << std::endl;

  return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now