c++ - 如何在一个数组中存储两个整数 (C++)

标签 c++ arrays

我想创建一个可以在单个单元格中存储 ID 和内容的数组。我想存储这些信息,以便我可以从一个输入中接收到两条信息。

目前我是这样做的:

int order[100];
int content[100];
int count = 0;

//method for adding a new piece of information
void setFrame(int nextOrder, int nextContent){
    order[count] = nextOrder;
    content[count] = nextContent;
    count++;
}

这可行,但我想要一种方法来调用单个数组,例如 Array[i],并从中获取两个整数。我怎样才能做到这一点?

最佳答案

您可以使用标准类 std::pair在 header 中声明 <utility>

#include <utility>

//...

std::pair<int, int> order[100];

void setFrame( int nextOrder, int nextContent )
{
    order[count++] = { nextOrder, nextContent };
}

关于c++ - 如何在一个数组中存储两个整数 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29110099/

相关文章:

c++ - 从数组中删除所有大于 n 的元素

arrays - 如何找到两个数组中的最小交换

javascript - 在按钮的onclick属性中传递php数组并在javascript中读取它

c++ - 运算符重载。 q = q1+q2,q1 被修改,但我希望 q1 和 q2 保持不变

c++ - rand() 和 random() 函数有什么区别?

c++ - 无法从伪终端读取

javascript - 在 V8 中从 C++ 代码创建自定义错误类

c++ - 不明白 array-1 是什么意思

javascript - 需要帮助来反转一串单词

c++ - 在多线程环境中使用 MPI_THREAD_SERIALIZED 时,所有 MPI 调用都需要锁吗?