How to take input and show output of an array of pairs?

Nov 3, 2014 at 2:41pm
I have written the following code using pair. But it gives error. How should I take input and show output of this array of pairs?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
    pair<int,int>ar[5];

    int a,b;

    for(int i=0; i<5; i++)
    {
        cin>>a>>b;
        ar.first(a);
        ar.second(b);
    }

    for(int i=0; i<5; i++)
    {
        cout<<ar[i].first<<" "<<ar[i].second<<endl;
    }

    return 0;
}
Nov 3, 2014 at 2:58pm
But it gives error.

This is really useless without telling us what errors.

You want to populate an array like this:

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

int main()
{
	std::vector<std::pair<int, int> > ar;

	int a(0);
	int	b(0);

	for (int i = 0; i < 5; i++)
	{
		std::cin >> a >> b;
		ar.push_back(std::make_pair(a, b));
	}

	return 0;
}
Last edited on Nov 3, 2014 at 2:59pm
Nov 3, 2014 at 2:59pm
The input is almost the same as the output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
    pair<int,int>ar[5];

    for(int i=0; i<5; i++)
    {
        cin>>ar[i].first>>ar[i].second;
    }

    for(int i=0; i<5; i++)
    {
        cout<<ar[i].first<<" "<<ar[i].second<<endl;
    }

    return 0;
}
Nov 3, 2014 at 3:10pm
Favour range-based for over the legacy for

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <utility>

int main()
{
    constexpr std::size_t N = 5 ;

    std::pair<int,int> my_legacy_array[N] { {}, {31,24}, {51,26}, {71,28} } ;
    my_legacy_array[0] = { 15, 26 } ;
    my_legacy_array[N-1] = { 98, 99 } ;

    // http://www.stroustrup.com/C++11FAQ.html#for
    for( const auto& p : my_legacy_array ) std::cout << '{' << p.first << ',' << p.second << "} " ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/429ebb25cfed230d
Nov 3, 2014 at 3:55pm
Trying really hard not to despise auto.
Nov 3, 2014 at 4:36pm
> Trying really hard not to despise auto.

IMHO, you shouldn't try too hard; it is mere syntactic sugar.
If, in your experience, search and replace makes maintenance easier, stay with search and replace.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <utility>

int main()
{
    constexpr std::size_t N = 5 ;

    std::pair<int,int double> my_legacy_array[N] { {}, {31,24}, {51,26}, {71,28} } ;
    my_legacy_array[0] = { 15, 26 } ;
    my_legacy_array[N-1] = { 98, 99 } ;

    // http://www.stroustrup.com/C++11FAQ.html#for
    for( const std::pair<int,int double>& p : my_legacy_array ) std::cout << '{' << p.first << ',' << p.second << "} " ;
    std::cout << '\n' ;
}
Nov 3, 2014 at 4:42pm
yea i know, but i think often it slows me down when i'm trying to understand other people's code. But i am fairly dumb.
Sorry for hijacking the thread OP.
Nov 3, 2014 at 5:20pm
> but i think often it slows me down when i'm trying to understand other people's code.

Yes.
Arguably, the case for identifiers that convey meaning becomes even stronger when type deduction is involved.

1
2
3
4
5
// for( const auto& p : my_legacy_array ) 
//    std::cout << '{' << p.first << ',' << p.second << "} " ; // p is a poor name

for( const auto& pair : my_legacy_array ) // much better
    std::cout << '{' << pair.first << ',' << pair.second << "} " ;
Topic archived. No new replies allowed.