tags:

views:

455

answers:

5

I'm trying to learn C++ and right now I'm writing a program that needs to output a list of pairs of integers.

What is the best way to handle this? I don't have the boost library available on our linux computers at school, so I don't believe I can use boost::tuple.

Any suggestions?

+1  A: 

What's wrong with an array?

JRL
Two arrays? The burden of having to keep them in sync! E.g try sorting this "list of pairs".
UncleBens
How about the hassle of keeping them in sync and the hassle of passing two lists around instead of one container of pairs.
Andrew Medico
I never mentioned 2 arrays. The question title is how to store a pair of integers. An int[2] array can do that. Then use whatever container you want to store the array (list, vector, whatever).
JRL
What's wrong with an array? In C++, almost everything. And you can't store arrays in standard library containers.
anon
I upped, because `int[N][2]` is a fine way to store `N` pairs of integers. No need for anything elaborated if a simple thing does it.
Johannes Schaub - litb
@Johannes: A fine way? Really? … Besides losing all semantic information? (at least `pair` says “this is logically a *pair* of values, while a two-dimensional array doesn’t even provide *that* information).
Konrad Rudolph
@Konrad, an int array with two members is a pair of integers. I don't see any semantic information lost. Furthermore, he wants it to be homogenous, making an array carry more information because it's more specific. If it's a class member, both a pair and an array are bad choices in my opinion, because ".first" and ".second" lose all information about their meaning anyway, which is crucial if it's used across multiple contexts: Both miss of local information, and high dependency make these solutions bad. At least with arrays, he can use symbolic indices (likewise, with `boost::tuple`).
Johannes Schaub - litb
I was about to up in agreement with Johannes when I realized that while arrays might be a solution to the problem, JRL didn't provided an answer but another question. int[X][2] is, IMHO, a good solution as any, because we don't know the restrictions or goals of the application. Vectors are better at storing lists, but only if we don't know the actual size of the list. If we do, arrays are faster and more compact, and somewhat easier to teach/understand/use.
Bruno Brant
@Bruno, agreed. JRL's answer is kind of "what's wrong with my solution?"
Johannes Schaub - litb
+13  A: 

Have a look at std::pair

http://www.sgi.com/tech/stl/pair.html

EDIT:

It's standard C++ and part of what is known as the STL (Standard Template Library). It's a collection of nice data structures that are generic (i.e. may be used to store any C++ object type). This particular structure is used to store a "tuple" or a pair of numbers together. It's basically an object with members "first" and "second" that refer to the first and second objects (of any type!) that you store in them.

So just declare an array of pair<int, int>, or better yet, use another STL type called the "vector" to make a dynamically-sized list of pair<int, int>: vector<pair<int, int> > myList.

Hey what do you know! A dynamically-sized list of pairs already exists and it's called a map! Using it is as simple as #include <map> and declaring a map<int, int> myMap!!!

EDIT:

Yes, as pointed out, a map well "maps" one object to another, so you cannot have repeated lefthand-side values. If that's fine then a map is what you're looking for, otherwise stick to the vector of pair.... or take a look at multimaps.

http://www.sgi.com/tech/stl/Map.html http://www.sgi.com/tech/stl/Multimap.html

Computer Guru
Note that outputting the pair will not be handled by `cout` (as it is for `int` or `double`) -- you will have to handle it separately.
dirkgently
In C++98 and C++03, you must use `vector<pair<int, int> >` (notice the space at the end!) because `>>` parses as "right shift". This will be fixed in C++0x.
ephemient
Yes, of course! Updated, thanks for catching that!
Computer Guru
@ephemient Interesting... I bet that generates a very helpful compiler error. ;)
nbolton
-1 because map isn't the same as treating pairs of values. Map does what it name says: maps one Key-value to another value. So, if using map, you can't have repetitions of the left-value.
Bruno Brant
@Bruno Yes you can - std::multimap
anon
+1  A: 

See std::pair at the STL site.

Dirk Eddelbuettel
The STL site? I wasn't aware there was an official STL site.
Trent
+6  A: 

Use std::pair?

#include <utility>
#include <iostream>

int main() {
    std::pair <int, int> p = std::make_pair( 1, 2 );
    std::cout << p.first << " " << p.second << std::endl;
}

You can make a vector of pairs:

typedef std::pair <int, int> IntPair;

...

std::vector <IntPair> pairs;
pairs.push_back( std::make_pair( 1, 2 ) );
pairs.push_back( std::make_pair( 3, 4 ) );
anon
Would you suggest std::pair over map?
Mithrax
Mithrax, map is just a wrapper around pair. It uses the pair code internally.
Computer Guru
@Mithrax That depends what you want to do with the pairs. If one is a key and one is a value, then you should use std::map, which is in fact implemented using std::pair.
anon
@Computer Guru map is not "just a wrapper" around pair. If anything it is a wrapper around a red-black balanced binary tree.
anon
Sure. Versus a vector<pair> which would be an array list wrapper of pair.Point is, at the end of the day, you can't suggest pair instead of map because they're apples and oranges. One of them is a basic datastructure that represents a tuple, the other USES the tuple to make an even larger structure :)Keep in mind the OP is a beginner to C++
Computer Guru
+1  A: 

While std::pair is the best approach to use, I'm surprised nobody mentioned pre-stl solution:

struct Pair {
    int first;
    int second;
};

It is worrying that people think that they need boost for such a trivial problem.

shura
Pre-STL like in pre-standard, previous millennium? And you wonder why nobody mentioned it? Additionally, your code lacks several important/nice features of `std::pair` so it doesn’t even serve for illustration purpose. In fact, what’s the purpose of mentioning this method at all?
Konrad Rudolph
Konrad, I did say that std::pair is better.I don't see why my code doesn't serve for illustration purpose.My main point was that sometimes people forget about simple solutions.
shura
If you're just trying to learn C++, this is easily the best answer presented. It's simple, uncomplicated and easy to adapt to other problems. Once they get used to struct, adapting to class and providing methods for things like MyObject.SecondValue is easy to teach. Combined with the array suggestion above (ie Pair[] MyValues) there's really no reason not to accept this answer.
FerretallicA