问题
I want to fill a vector with 8 pairs. Each pair represents the moves in x and y coordinates a knight in a game of chess can make. At the moment I'm doing it like this
vector<pair<int,int>> moves;
pair<int,int> aPair;
aPair.first = -2;
aPair.second = -1;
moves.push_back(aPair);
aPair.first = -2;
aPair.second = 1;
moves.push_back(aPair);
aPair.first = -1;
aPair.second = -2;
moves.push_back(aPair);
aPair.first = -1;
aPair.second = 2;
moves.push_back(aPair);
aPair.first = 1;
aPair.second = -2;
moves.push_back(aPair);
aPair.first = 1;
aPair.second = 2;
moves.push_back(aPair);
aPair.first = 2;
aPair.second = -1;
moves[6].push_back(aPair);
aPair.first = 2;
aPair.second = 1;
moves.push_back(aPair);
I'm doing this to learn about the Std library. This seems like a hopelessly inefficient way of solving this problem.
Anyone have a more elegant solution?
回答1:
Loops to the rescue:
for(int k = 0; k < 2; k++)
for(int i = -1; i < 2; i += 2)
for(int j = -1; j < 2; j+= 2)
result.push_back(make_pair(i * (k+1), j * (((k + 1) % 2) + 1)));
Output: http://ideone.com/2B0F9b
回答2:
If you have C++11 (otherwise you can't write >>
), you can use the following:
vector<pair<int,int>> moves = {
{-2, -1},
{-2, 1},
{-1, -2},
{-1, 2},
{ 1, -2},
{ 1, 2},
{ 2, -1},
{ 2, 1}
};
回答3:
In C++98/03:
moves.push_back(std::make_pair(-2, -1));
In C++11:
moves.emplace_back(-2, -1);
Alternatively in C++11:
std::vector<std::pair<int, int>> moves = { { -2, -1}, ... };
回答4:
If you don't have C++11 you can utilize make_pair, pre-allocate the space for the vector without initializing the elements using reserve, and then utilize push_back without new allocations being done.
For example:
vector<pair<int,int> > moves;
moves.reserve(8);
moves.push_back(make_pair(-2, -1));
// and so on
Even if you have C++11 this technique is useful if you need to compute the elements on the fly rather than hard code them.
回答5:
Try that:
vector<pair<int,int>> moves{{-2, -1}, {2, 1}, {-1, -2}, {-1, 2},
{1, -2}, {1, 2}, {2, -1}, {2, 1}};
Initializer list together with Uniform Initialization gives a lot of power in C++11.
回答6:
Here's another method of doing the same thing.
template <class VectorClass>
class CreateVector
{
public:
typedef typename VectorClass::value_type value_type;
CreateVector(const value_type& value)
{
mVector.push_back(value);
}
CreateVector& operator()(const value_type& value)
{
mVector.push_back(value);
return *this;
}
inline operator VectorClass() const
{
return mVector;
}
private:
VectorClass mVector;
};
Usage:
vector<pair<int,int>> moves = CreateVector<vector<pair<int,int> > >
(make_pair(1,2))
(make_pair(2,3))
(make_pair(3,4))
(make_pair(4,5));
EDIT: Provided you're not using C++11, this would be one way. Otherwise, I would suggest to go the way @ipc suggested.
回答7:
If you're using C++11, you might want to consider std::array instead of std::vector. Like a normal array, the std array has a fixed number of elements and makes more conceptual sense if you know in advance how much data you use.
回答8:
Hopefully a more readable version with loops:
vector<pair<int, int>> result;
for(int moveX=1; moveX<=2; moveX++)
{
for(int signX=-1; signX<=1; signX+=2)
{
for(int signY=-1; signY<=1; signY+=2)
{
result.push_back(make_pair(moveX*signX, (3-moveX)*signY));
}
}
}
Full program produces the following vector:
{-1, -2},
{-1, 2},
{1, -2},
{1, 2},
{-2, -1},
{-2, 1},
{2, -1},
{2, 1},
来源:https://stackoverflow.com/questions/13406790/filling-a-vector-of-pairs