In C++, Qt, the output of character arrays, non-character arrays, and vector containers

core

1. There are two situations for outputting array data in C++: character array and non-character array.
2. The char type is originally used to store characters. It is possible to directly use the array name; the int type is similar (the variable name is the first of the array). Address)
3. The char type must have quotation marks ("asd",'asd'), while int does not

vector container

cout << Array name-array value-{1, 2, 3, 4, 5, 9, 6}

qDebug() << testArray;

cout << & array name-address (similar to local variables)

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;
}

Character array

cout << array name-string

//以字符的形式初始化
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 << forced type conversion-address

char str_force[10] = { '1','2' };//输出地址需要强制类型转换
cout << static_cast <void *> (str_force) << endl;

Non-character array

cout << array name-address

1 int a[10]={
    
    1,2,3};
2 cout << a <<endl ; //按16进制输出a的值(地址

cout << loop-string

int c[10] = {
    
    1,2,3};//剩下的默认为0
for (int i = 0; i < 10; i++)
	cout << c[i] << " " << endl;

verification

#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;
}

Guess you like

Origin blog.csdn.net/qq_43641765/article/details/111401869