C++ 填充成对的向量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13406790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:15:46  来源:igfitidea点击:

Filling a vector of pairs

c++stl

提问by Q-bertsuit

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

我想用 8 对填充一个向量。每对代表在 x 和 y 坐标中一个骑士在国际象棋游戏中可以进行的移动。目前我正在这样做

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.

我这样做是为了了解 Std 库。这似乎是解决这个问题的一种非常低效的方法。

Anyone have a more elegant solution?

有人有更优雅的解决方案吗?

采纳答案by hate-engine

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

输出:http: //ideone.com/2B0F9b

回答by ipc

If you have C++11 (otherwise you can't write >>), you can use the following:

如果你有 C++11(否则你不能写>>),你可以使用以下内容:

vector<pair<int,int>> moves = {
  {-2, -1},
  {-2,  1},
  {-1, -2},
  {-1,  2},
  { 1, -2},
  { 1,  2},
  { 2, -1},
  { 2,  1}
};

回答by Kerrek SB

In C++98/03:

在 C++98/03 中:

moves.push_back(std::make_pair(-2, -1));

In C++11:

在 C++11 中:

moves.emplace_back(-2, -1);

Alternatively in C++11:

或者在 C++11 中:

std::vector<std::pair<int, int>> moves = { { -2, -1}, ... };

回答by Josh Heitzman

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.

如果您没有 C++11,您可以使用make_pair,预先为向量分配空间而不使用 Reserve 初始化元素,然后使用 push_back 而不进行新的分配。

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.

即使您有 C++11,如果您需要动态计算元素而不是硬编码它们,这种技术也很有用。

回答by Mateusz Pusz

Try that:

试试看:

vector<pair<int,int>> moves{{-2, -1}, {2, 1}, {-1, -2}, {-1, 2},
                            {1, -2},  {1, 2}, {2, -1},  {2, 1}};

Initializer listtogether with Uniform Initialization gives a lot of power in C++11.

初始化列表和统一初始化在 C++11 中提供了很多功能。

回答by Vite Falcon

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.

编辑:如果您不使用 C++11,这将是一种方式。否则,我建议按照@ipc 建议的方式进行。

回答by Alexander Duchene

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.

如果您使用的是 C++11,您可能需要考虑 std::array 而不是 std::vector。与普通数组一样,std 数组具有固定数量的元素,如果您事先知道您使用了多少数据,那么它在概念上更有意义。

回答by Serge Rogatch

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 programproduces the following vector:

完整程序产生以下向量:

{-1, -2},
{-1, 2},
{1, -2},
{1, 2},
{-2, -1},
{-2, 1},
{2, -1},
{2, 1},