4 Replies - 518 Views - Last Post: 08 March 2013 - 06:34 AM Rate Topic: -----

#1 jinified  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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>

std::pair<std::string, int>readin(std::string s, int i);

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

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






The output seems ok, but is there a better way going about this.
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  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#3 jinified  Icon User is offline

  • New D.I.C Head

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

Re: Reading input into vector of pair

Posted 08 March 2013 - 12:12 AM

Thanks for the suggestion.
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,159
  • 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);
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1