How can I create a vector and array of pair?

We can define pair as following but how to declare a vector and a array of pair?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <utility>
#include <algorithm>
#include <vector>

using namespace std;

int main ()
{
    
    pair <int,char> p;
    p.first=13123;
    p.second='a';
    

    cout<<p.first<<" "<<p.second<<endl;

    return 0;
}
1
2
3
4
5
6
7
constexpr std::size_t N = 25 ;

std::vector< std::pair<int,char> > my_vector(N) ;

std::array< std::pair<int,char>, N > my_array {{}} ; // #include <array>

std::pair<int,char> my_legacy_array[N] = {} ;
Thank you.

Please tell me one more thing, how do I print a,b as a pair from vector v?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
    vector<pair<int,int>> v;

    int a,b;

    for(int i=0; i<5; i++)
    {
        cin>>a>>b;
        v.push_back(pair<int, int> (a,b));
    }
    
    //How do i print a,b as a pair from vector v?
    return 0;
}
Last edited on
Topic archived. No new replies allowed.