核心
1、C++中输出数组数据分两种情况:字符型数组和非字符型数组
2、char类型本就是用来存储字符的,直接用数组名是可以的;而int类型类似(变量名就是数组首地址)
3、char类型必须有引号(“ asd”、'asd '),而int没有
vector容器
cout << 数组名——数组的值——{1,2,3,4,5,9,6}
qDebug() << testArray;
cout << &数组名——地址(类似局部变量)
qDebug() << &testArray;——0x78fc40
int a = 1;
qDebug() << "a的地址:" << &a;
vector<double> array_1 = {
1,2,3,1,2,3 };
//直接输出数组
cout << array_1;
//遍历数组中的值(普通法)
for (int i = 0; i < array_1.size(); i++) {
cout << array_1[i];
}
cout << endl;
//遍历数组中的值(begin、end法法)迭代器法
for (auto i = array_1.begin(); i < array_1.end(); i++) {
cout << *i ;
}
cout << endl;
}
字符型数组
cout << 数组名——字符串
//以字符的形式初始化
char str[10] = {
'1','2' };//12
//以字符串的形式初始化
char str_array[10] = {
"jiang"};//jiang
//初始化可以不加{},也是可以,但是最好不用
char str_3[10] = "jiang";//jiang
cout << str << endl << str_array << endl;
cout << 强制类型转换——地址
char str_force[10] = { '1','2' };//输出地址需要强制类型转换
cout << static_cast <void *> (str_force) << endl;
非字符型数组
cout << 数组名——地址
1 int a[10]={
1,2,3};
2 cout << a <<endl ; //按16进制输出a的值(地址
cout << 循环——字符串
int c[10] = {
1,2,3};//剩下的默认为0
for (int i = 0; i < 10; i++)
cout << c[i] << " " << endl;
验证
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a[3] = {
1};//等同于a[0] = 1;
//两种方式都可以
cout << sizeof(a) << endl << sizeof a << endl;//整个数组的字节数,3*4
cout << sizeof a[0] << endl; //元素的字节数(长度),4
int b[3] = {
0,1,2 };//初始化数组,
b[2] = 0;//一个数组不可以整体赋值给另一个数组,但是可以vectro拷贝;而且可以通过下标赋值
cout << b[2] << endl;
char str[10] = {
'1','2' };//12
char str_array[10] = {
"jiang"};//jiang
cout << str << endl << str_array << endl;
char str_force[10] = {
'1','2' };//输出地址需要强制类型转换
cout << static_cast <void *> (str_force) << endl;
int c[10] = {
1,2,3};//剩下的默认为0
for (int i = 0; i < 10; i++)
cout << c[i] << " " << endl;
system("pause");
return 0;
}