Ben*_*n91 5 c++ arrays vector coordinates
首先,我对 C++ 很陌生,所以我可能不得不深入研究伪代码和/或 Python 来解释我想要做什么......
我正在尝试为多个精灵存储动画的每一帧的 X 和 Y 坐标对。我设想这类似于以下内容 - 假设 PLAIN == 1 (使用枚举):
animationFrames[PLAIN][0] = { 20, 50 }
animationFrames[PLAIN][1] = { 25, 55 }
Run Code Online (Sandbox Code Playgroud)
等等。我基本上希望能够使用相关精灵的 ID 查询 animationFrames 并接收一组 X、Y 坐标以进行迭代。我觉得这很棘手。这是我的尝试,但不起作用...
std::vector< std::vector< std::pair<int, int> > > frames = {
{
{ 1, 1 }, { 2, 2 } // two frames for sprite A
},
{
{ 3, 3 }, { 4, 4 } // two frames for sprite B
}
};
Run Code Online (Sandbox Code Playgroud)
这会导致以下错误消息:
prog.cpp: In function 'int main()':
prog.cpp:15: error: braces around initializer for non-aggregate type 'std::vector<std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > >, std::allocator<std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > > > >'
Run Code Online (Sandbox Code Playgroud)
我尝试了向量、对和数组的各种突变,但我似乎无法弄清楚。
提前致谢!
我认为您的编译器可能无法处理 C++11 标准,这意味着它不支持大括号初始化。您可以将项目一项一项地添加:
std::vector<std::vector<std::pair<int, int> > > frames(2);
std::vector<std::pair<int, int> > &v1 = frames[0];
v1.push_back(std::pair<int, int>(1, 1));
v1.push_back(std::pair<int, int>(2, 2));
std::vector<std::pair<int, int> > &v2 = frames[1];
v2.push_back(std::pair<int, int>(3, 3));
v2.push_back(std::pair<int, int>(4, 4));
Run Code Online (Sandbox Code Playgroud)
它丑陋得多,但应该可行。另一方面,如果您的编译器确实支持 C++11,您甚至不需要=, 并且可以删除一些空格:
std::vector<std::vector<std::pair<int, int>>> frames {
{
{ 1, 1 }, { 2, 2 } // two frames for sprite A
},
{
{ 3, 3 }, { 4, 4 } // two frames for sprite B
}
};
Run Code Online (Sandbox Code Playgroud)
请注意,某些较旧的编译器可能需要命令行参数才能启用 C++11 支持。例如,旧版本的 GCC (g++) 需要-std=c++11.
| 归档时间: |
|
| 查看次数: |
5370 次 |
| 最近记录: |