题
有没有办法在 C++ 中打印数组?
我正在尝试创建一个函数来反转用户输入数组,然后将其打印出来。我尝试用谷歌搜索这个问题,似乎 C++ 无法打印数组。这不可能是真的吧?
解决方案
你不能只遍历元素?像这样:
for (int i = numElements - 1; i >= 0; i--)
cout << array[i];
请注意:由于马克西姆Egorushkin指出的,这可能溢出。见下面他的评论为更好的解决方案。
其他提示
使用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";
}
我建议用鱼骨操作?
for (auto x = std::end(a); x != std::begin(a); )
{
std::cout <<*--x<< ' ';
}
(你能发现它?)
除了基于 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"));
}
这会生成以下内容:
./a.out
1
3
5
7
然而,这对于您的需求来说可能有点过分了。一个直接的 for 循环可能就是您所需要的,尽管 litb 的模板糖 也很不错。
编辑:忘记了“反向打印”的要求。这是一种方法:
#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"));
}
和输出:
$ ./a.out
7
5
3
1
编辑:C++14 更新使用数组迭代器函数简化了上述代码片段,例如 std::开始() 和 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"));
}
有声明阵列和数组是不的声明,否则创建的,特别是在使用new
:
int *p = new int[3];
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;
}
这里的问题是,它不接受指针(明显)。最简单的解决方案,我想,就是用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;
}
可以,当然,也使这个模板接受其他类型的载体。
大多数在C ++中常用的库的不能打印的阵列,本身。您会通过它手动必须循环并打印出的每个值。
印刷阵列和倾倒许多不同类型的对象是高级语言的特征。
这当然是!您会通过阵列必须循环地和单独地打印出每一个项目。
C ++可以打印任何你想要的,如果你的程序是这样做的。你必须要经过自己的数组打印每个元素。
这可能会帮助 //打印的阵列
for (int i = 0; i < n; i++)
{cout << numbers[i];}
n是阵列的尺寸
我的简单的答案是:
#include <iostream>
using namespace std;
int main()
{
int data[]{ 1, 2, 7 };
for (int i = sizeof(data) / sizeof(data[0])-1; i >= 0; i--) {
cout << data[i];
}
return 0;
}
或者你可以使用这个: https://github.com/gileli121/VectorEx
可以很容易地显示与DisplayVector_1d阵列()。 它发展下视觉工作室2015吨只能视窗(Windows上测试7 64位)。
下你要找这个库是DisplayVector_1d。我
它会显示在列表视图GUI阵列。这就像在AutoIt中的_ArrayDisplay功能。
下面是一个例子如何使用此:
// includes.functions
#include <windows.h>
#include "VectorEx\VectorEx.h"
#include "VectorEx\VectorDisplay.h"
using namespace vectorex;
using namespace vectordisplay;
/*
This example shows how to use display std::vector array datatype
with vectordisplay::DisplayVector_1d
*/
int main()
{
std::vector<std::string> stringVector;
stringVector.push_back("Bob");
stringVector.push_back("Fob");
stringVector.push_back("Rob");
stringVector.push_back("Nob");
DisplayVector_1d(&stringVector);
return 0;
}
请注意,我刚开始在这个项目上。欢迎您来提高代码和修正错误