# Reading input into vector of pair

Page 1 of 1

## 4 Replies - 1677 Views - Last Post: 08 March 2013 - 06:34 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=314718&amp;s=4c2bbfea56b3350d72c3273a9cf9d28f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jinified

Reputation: 0
• Posts: 19
• Joined: 26-February 13

# Reading input into vector of pair

Posted 07 March 2013 - 10:26 PM

Greetings!!!

This is the exercise I did:
Write a program to read a sequence of strings and ints,
storing each into a pair. Store the pairs in a vector.

Here is my code:
```#include <utility>
#include <vector>
#include <iostream>

int main()
{
std::string s;
int i(0);
std::vector<std::pair<std::string, int>>store;
while(std::cin>>s>>i)
{
}
std::cout<<store[0].first <<'\n';
std::cout <<store[0].second<<'\n';
}

{
std::pair<std::string, int>couple={s, i};
return couple;
}

```

Just curious if any of the experts here could point out a simpler or more optimised solution for this problem.
THanks

Is This A Good Question/Topic? 0

## Replies To: Reading input into vector of pair

### #2 jjl

• Engineer

Reputation: 1130
• Posts: 4,686
• Joined: 09-June 09

## Re: Reading input into vector of pair

Posted 07 March 2013 - 11:24 PM

Something like this doesn't really need to much optimizing, you shouldn't worry about it.

I suppose if any thing you could add a little convenience to the code.
```#include <iostream>
#include <vector>
#include <utility>

std::istream &operator>>(std::istream &in, std::pair<std::string, int> &p) {
in>>p.first>>p.second;
return in;
}

std::ostream &operator<<(std::ostream &out, const std::pair<std::string, int> &p) {
out<<p.first<<' '<<p.second<<std::endl;
return out;
}

int main() {
std::vector<std::pair<std::string, int> >pairs;
std::pair<std::string, int> temp;

while(std::cin>>temp) {
pairs.push_back(temp);
std::cout<<temp;
}

return 0;
}

```

### #3 jinified

Reputation: 0
• Posts: 19
• Joined: 26-February 13

## Re: Reading input into vector of pair

Posted 08 March 2013 - 12:12 AM

Thanks for the suggestion.

### #4 jjl

• Engineer

Reputation: 1130
• Posts: 4,686
• Joined: 09-June 09

## Re: Reading input into vector of pair

Posted 08 March 2013 - 12:37 AM

If you want to get a handle on the efficiency of your code, try and find out how many copies of the same pair that's created for each input. In other words, how many times does your input get copied before it gets stored in the vector.

Hint: Take a look at the implementation of push_back, and returning structures by value.

Once you find this number, you should try to minimize it as low as possible.

Again, optimization isn't necessary here, but it is in other areas and it is good to know about reducing pointless copies of the same data.

This post has been edited by jjl: 08 March 2013 - 12:39 AM

### #5 Skydiver

• Code herder

Reputation: 4376
• Posts: 14,124
• Joined: 05-May 12

## Re: Reading input into vector of pair

Posted 08 March 2013 - 06:34 AM

jjl makes a very good point with regards to efficiency. I remember with the early versions of in house homegrown C++ libraries prior to the existence of STL, a lot of code reviewing needed to be done to make guess-timate of container efficiency and whether it would be better to pass in a pointer to an object or a copy of the object. Looking into the STL code can be very daunting to a beginner or the lazy, though. I'm in the lazy category, so what I ended up doing was writing this evil class:

```class Counter
{
private:
static int _instances;

public:
Counter()
{
std::cout << ++_instances << std::endl;
}
};

Counter::_instances = 0;

```

And then tried playing around with the container class using the type. For example:
```vector<Counter> vec;
for(int i = 0; i < 10; i++)
{
Counter temp;
vec.push_back(temp);
}

```