Bot*_*000 50
难道你不能迭代元素吗?像这样:
for (int i = numElements - 1; i >= 0; i--)
cout << array[i];
Run Code Online (Sandbox Code Playgroud)
注意:正如Maxim Egorushkin指出的那样,这可能会溢出.请参阅下面的评论以获得更好的解决方案
Mar*_*ork 47
使用STL
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
int main()
{
std::vector<int> userInput;
// Read until end of input.
// Hit control D
std::copy(std::istream_iterator<int>(std::cin),
std::istream_iterator<int>(),
std::back_inserter(userInput)
);
// Print in Normal order
std::copy(userInput.begin(),
userInput.end(),
std::ostream_iterator<int>(std::cout,",")
);
std::cout << "\n";
// Print in reverse order:
std::copy(userInput.rbegin(),
userInput.rend(),
std::ostream_iterator<int>(std::cout,",")
);
std::cout << "\n";
// Update for C++11
// Range based for is now a good alternative.
for(auto const& value: userInput)
{
std::cout << value << ",";
}
std::cout << "\n";
}
Run Code Online (Sandbox Code Playgroud)
fre*_*low 30
我建议使用鱼骨操作员吗?
for (auto x = std::end(a); x != std::begin(a); )
{
std::cout <<*--x<< ' ';
}
Run Code Online (Sandbox Code Playgroud)
(你能发现它吗?)
Voi*_*oid 13
除了基于for循环的解决方案,您还可以使用ostream_iterator <>.这是一个利用(现已退役)SGI STL参考中的示例代码的示例:
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
short foo[] = { 1, 3, 5, 7 };
using namespace std;
copy(foo,
foo + sizeof(foo) / sizeof(foo[0]),
ostream_iterator<short>(cout, "\n"));
}
Run Code Online (Sandbox Code Playgroud)
这会生成以下内容:
./a.out
1
3
5
7
Run Code Online (Sandbox Code Playgroud)
但是,这可能对你的需求有些过分.虽然litb的模板糖也非常好,但你可能只需要一个直接的for循环.
编辑:忘记"反向打印"要求.这是一种方法:
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
short foo[] = { 1, 3, 5, 7 };
using namespace std;
reverse_iterator<short *> begin(foo + sizeof(foo) / sizeof(foo[0]));
reverse_iterator<short *> end(foo);
copy(begin,
end,
ostream_iterator<short>(cout, "\n"));
}
Run Code Online (Sandbox Code Playgroud)
和输出:
$ ./a.out
7
5
3
1
Run Code Online (Sandbox Code Playgroud)
编辑:C++ 14更新,使用数组迭代器函数(如std :: begin()和std :: rbegin())简化上面的代码片段:
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
short foo[] = { 1, 3, 5, 7 };
// Generate array iterators using C++14 std::{r}begin()
// and std::{r}end().
// Forward
std::copy(std::begin(foo),
std::end(foo),
std::ostream_iterator<short>(std::cout, "\n"));
// Reverse
std::copy(std::rbegin(foo),
std::rend(foo),
std::ostream_iterator<short>(std::cout, "\n"));
}
Run Code Online (Sandbox Code Playgroud)
有申报阵列和阵列那些不申报,但在其他方面创造,特别是使用new
:
int *p = new int[3];
Run Code Online (Sandbox Code Playgroud)
具有3个元素的数组是动态创建的(也3
可以在运行时计算),并指定一个指向它的大小已从其类型中删除的指针p
.您无法再获得打印该阵列的大小.仅接收指向它的指针的函数因此不能打印该数组.
打印声明的数组很容易.您可以使用它sizeof
来获取它们的大小并将该大小传递给函数,包括指向该数组元素的指针.但是您也可以创建一个接受该数组的模板,并从其声明的类型中推导出它的大小:
template<typename Type, int Size>
void print(Type const(& array)[Size]) {
for(int i=0; i<Size; i++)
std::cout << array[i] << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这个问题是它不会接受指针(显然).我认为最简单的解决方案就是使用std::vector
.它是一个动态的,可调整大小的"数组"(具有您期望从真实语义中获得的语义),它具有size
成员函数:
void print(std::vector<int> const &v) {
std::vector<int>::size_type i;
for(i = 0; i<v.size(); i++)
std::cout << v[i] << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
当然,您也可以将此模板设为接受其他类型的矢量.