C++、Qt中,字符型数组和非字符型数组、vector容器的输出,

5 篇文章 0 订阅

核心

1、C++中输出数组数据分两种情况:字符型数组和非字符型数组
2、char类型本就是用来存储字符的,直接用数组名是可以的;而int类型类似(变量名就是数组首地址)
3、char类型必须有引号(“ asd”、'asd '),而int没有

vector容器

vector<int> testArray = {1234596};
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;
}

c.begin() 返回一个迭代器,它指向容器c的第一个元素

c.end() 返回一个迭代器,它指向容器c的最后一个元素的下一个位置

c.rbegin() 返回一个逆序迭代器,它指向容器c的最后一个元素

c.rend() 返回一个逆序迭代器,它指向容器c的第一个元素前面的位置

在这里插入图片描述

sort进行排序,顺序输出和倒序输出

sort(c.begin(), c.end());//顺序,仅需要知道begin和end
sort(c.rbegin(), c.rend());

for (auto i = c.begin(); i < c.end(); i++)
{
	cout << *i << 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;//error

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

二维vector容器

//#define x 3//宏定义数组每列存储元素的最多个数
//#define y 4

int main()
{
	const int x = 3, y = 4;//必须是常量

	int a[x][y], b[100], i, j, n, m;//定义i、j,之后的for循环可以都是ij循环开始的
	
	for (i = 0; i < x; i++)
	{
		for (j = 0; j < y; j++)
		{
			//scanf_s("%d", &a[i][j]);
			//输入二维数组存储元素,这一步可以直接创建二维数组

			cin >> a[i][j];
		}
	}

	for (i = 0; i < x; i++)
	{
		for (j = 0; j < y; j++)
		{
			//printf("a[%d][%d]=%d  ", i, j, a[i][j]);//输出二维数组存储元素

			cout << a[i][j];
		}
		printf("\n");//以每行y个元素的格式输出
	}

	vector<vector<double> > a(3, vector<double>(2, 1));

	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 2; j++)
		{
			cout << a[i][j] ;
		}
		cout << endl;
	}

	system("pause");
	return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值