Bonk
Bonk

Reputation: 1949

problem with array of std::pair in c++?

I was playing around with some code today, and I came across the idea of putting a bunch of pairs in an array, but I havn't been able to get it to work. Here are the code:

#include <utility>
#include <iostream>
std::pair<double,double> * pairArr;
int main(){
   pairArr = new std::pair<double,double> [3];   //creating the array

   pairArr [0] = make_pair(1.0,1.2);             //Filling arbitrary numbers
   pairArr [1] = make_pair(0.2,1.4);
   pairArr [2] = make_pair(0.8,1.3);

   std::cout<< pair[0].first << pair[1].second << pair[2].first   //Output
   return 0;
}

the output of this program is not all what I had input.

Upvotes: 1

Views: 17221

Answers (1)

Cubbi
Cubbi

Reputation: 47408

The program as posted does not compile.

Changing the output to

std::cout << pairArr[0].first << pairArr[1].second << pairArr[2].first;

produces the expected result, 11.40.8, that is 1 1.4 0.8 without spaces: https://ideone.com/XAPHX

Upvotes: 9

Related Questions