gpt4 book ai didi

c++ - 如何在 C++ 中遍历数字列表

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:48:32 25 4
gpt4 key购买 nike

我如何遍历数字列表,有多少种不同的方法?

我认为可行的方法:

#include <cstdlib>
#include <iostream>
#include <list>


using namespace std;


int main()

{
int numbers[] = {2, 4, 6, 8};
int i = 0;
for(i=0; i< numbers.size();i++)
cout << "the current number is " << numbers[i];


system("pause");

return 0;

}

我在 for 循环行上得到一个错误:

request for member 'size' in 'numbers', which is of non-class type 'int[4]'

最佳答案

与许多现代语言不同,普通 C++ 数组没有 .size() 函数。根据存储类型,您有许多选项可以循环访问列表。

一些常见的存储选项包括:

// used for fixed size storage. Requires #include <array>
std::array<type, size> collection;

// used for dynamic sized storage. Requires #include <vector>
std::vector<type> collection;

// Dynamic storage. In general: slower iteration, faster insert
// Requires #include <list>
std::list<type> collection;

// Old style C arrays
int myarray[size];

您的迭代选项将取决于您使用的类型。如果您使用的是普通的旧 C 数组,您可以将大小存储在其他地方或根据其类型的大小计算数组的大小。 Calculating the size of an array has a number of drawbacks outlined in this answer by DevSolar

// Store the value as a constant
int oldschool[10];
for(int i = 0; i < 10; ++i) {
oldschool[i]; // Get
oldschool[i] = 5; // Set
}

// Calculate the size of the array
int size = sizeof(oldschool)/sizeof(int);
for(int i = 0; i < size; ++i) {
oldschool[i]; // Get
oldschool[i] = 5; // Set
}

如果您使用任何提供 .begin().end() 函数的类型,您可以使用它们来获得一个被认为是好的风格的迭代器在 C++ 中与基于索引的迭代相比:

// Could also be an array, list, or anything with begin()/end()
std::vector<int> newschool;

// Regular iterator, non-C++11
for(std::vector<int>::iterator num = newschool.begin(); num != newschool.end(); ++num) {
int current = *num; // * gets the number out of the iterator
*num = 5; // Sets the number.
}

// Better syntax, use auto! automatically gets the right iterator type (C++11)
for(auto num = newschool.begin(); num != newschool.end(); ++num) {
int current = *num; // As above
*num = 5;
}

// std::for_each also available
std::for_each(newschool.begin(), newschool.end(), function_taking_int);

// std::for_each with lambdas (C++11)
std::for_each(newschool.begin(), newschool.end(), [](int i) {
// Just use i, can't modify though.
});

vector 也很特别,因为它们被设计为数组的直接替代品。您可以像使用 .size() 函数遍历数组一样遍历 vector 。然而,这在 C++ 中被认为是不好的做法,您应该尽可能使用迭代器:

std::vector<int> badpractice;
for(int i = 0; i < badpractice.size(); ++i) {
badpractice[i]; // Get
badpractice[i] = 5; // Set
}

C++11(新标准)还带来了新的奇特范围,因为它应该适用于提供 .begin().end()< 的任何类型。但是:编译器支持可能会因该功能而异。您还可以使用 begin(type)end(type) 作为替代。

std::array<int, 10> fancy;
for(int i : fancy) {
// Just use i, can't modify though.
}

// begin/end requires #include <iterator> also included in most container headers.
for(auto num = std::begin(fancy); num != std::end(fancy); ++num) {
int current = *num; // Get
*num = 131; // Set
}

std::begin 还有另一个有趣的属性:它适用于原始数组。这意味着您可以在数组和非数组之间使用相同的迭代语义(您仍然应该更喜欢标准类型而不是原始数组):

int raw[10];
for(auto num = std::begin(raw); num != std::end(raw); ++num) {
int current = *num; // Get
*num = 131; // Set
}

如果您想在循环中从集合中删除项目,您还需要小心,因为调用 container.erase() 会使所有现有的迭代器无效:

std::vector<int> numbers;
for(auto num = numbers.begin(); num != numbers.end(); /* Intentionally empty */) {
...

if(someDeleteCondition) {
num = numbers.erase(num);
} else {
// No deletition, no problem
++num;
}
}

此列表远非全面,但如您所见,有很多方法可以迭代集合。一般来说,除非您有充分的理由不这样做,否则更喜欢迭代器。

关于c++ - 如何在 C++ 中遍历数字列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14350886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com