How to create, initialize and print a vector of pairs in C++

2 Answers

0 votes
#include <iostream>
#include <vector>

void print(const std::vector<std::pair<int, int>> vec) {
    for (int i = 0; i < vec.size(); i++) {
        std::cout << vec[i].first << ", " << vec[i].second << std::endl;
    }
}

int main()
{
    std::vector<std::pair<int, int>> vec = { {3, 8}, {5, 2}, {-4, 7} };
    
    print(vec);
}
  
  
  
  
/*
run:
  
3, 8
5, 2
-4, 7
  
*/

 



answered Jun 18, 2024 by avibootz
edited Jun 19, 2024 by avibootz
0 votes
#include <iostream>
#include <vector>

void print(const std::vector<std::pair<int, int>> vec) {
    for (const auto& val : vec) {
        std::cout << val.first << ", " << val.second << std::endl;
    }
}

int main()
{
    std::vector<std::pair<int, int>> vec = { {3, 8}, {5, 2}, {-4, 7} };
    
    print(vec);
}
  
  
  
  
/*
run:
  
3, 8
5, 2
-4, 7
  
*/

 



answered Jun 18, 2024 by avibootz
edited Jun 19, 2024 by avibootz

Related questions

1 answer 82 views
1 answer 93 views
1 answer 148 views
1 answer 140 views
140 views asked Feb 15, 2018 by avibootz
...