首页 技术 正文
技术 2022年11月8日
0 收藏 414 点赞 2,057 浏览 1764 个字

c/c++ 数组和指针

知识点

1,数组就是指针,对应代码里的test1

2,用auto声明,得到的是指针,对应代码里的test2

3,用decltype声明,得到的不是指针 ,对应代码里的test3

4,用指针模拟end ,对应代码里的test4

5,标准库函数std::begin,std::end,对应代码里的test5

6,ptrdiff_t和size_t,ptrdiff是数组下标相加减的值的类型,size_t是数组下标的类型,对应代码里的test6

7,数组的下标可以是负值,标准库的string,vector的下标也可以是负值,对应代码里的test7

8,用数组初始化vector,注意生成的vector里的元素不包括第二个参数,对应代码里的test8

#include <iostream>
#include <vector>using namespace std;int main(){ //test1 数组就是指针
/*
string ar[] = {"aa","bb"};
string* p = ar;
*p = "cc";
string* p1 = &ar[1];
*p1 = "dd";
for(auto s : ar){
cout << s;
}
cout << endl;
*/ //test2 用auto声明,得到的是指针
/*
int ia[] = {1,2,3,5};
//ia1为整数指针
auto ia1(ia);
*ia1 = 9;
auto ia2(&ia[3]);
*ia2 = 8;
for(auto s : ia){
cout << s << ",";
}
cout << endl;
*/ //test3 用decltype声明,得到的不是指针
/*
int ia[3];
decltype(ia) ia3 = {2,3,4};
//ia3 = &ia[0];//编译错误
ia3[2] = 9;
for(auto s : ia3){
cout << s << ",";
}
cout << endl;
*/ //test4 用指针模拟end
/*
int arr[] = {0,1,2};
int* end = &arr[3];
for(int* beg = arr;beg != end; ++ beg){
cout << *beg;
}
cout << endl;
*/ //test5 标准库函数std::begin,std::end
/*
int arr[] = {0,1,2};
int* beg = std::begin(arr);
//int* end = &arr[3];
int* end = std::end(arr);
for(;beg != end; ++ beg){
cout << *beg;
}
cout << endl;
*/ //test6 ptrdiff_t和size_t
//ptrdiff是数组下标相加减的值的类型,size_t是数组下标的类型
/*
int arr[] = {1,2,3,4,5};
int* b = std::begin(arr);
int* e = std::end(arr);
ptrdiff_t juli = e - b - 1;
cout << juli << endl;
size_t sz = juli;
cout << arr[sz] << endl;
*/ //test7 数组的下标可以是负值,标准库的string,vector的下标也可以是负值
/*
int ia[] = {1,2,3,4,5};
int* p = &ia[2];
int j = p[1];//相当于*(p + 1),也就是ia[3]
cout << j << endl;
int k = p[-2];//相当于*(p - 2),也就是ia[0]
cout << k << endl;
string s("abcde");
char* p1 = &s[2];
cout << p1[-1] << endl;
vector<int> v{1,2,3,4};
int* p2 = &v[3];
cout << p2[-2] << endl;
*/ //test8 用数组初始化vector,注意生成的vector里的元素不包括第二个参数
/*
int ia[] = {0,1,2,3,4,5,6};
vector<int> v(std::begin(ia), std::end(ia));
for(auto s : v){
cout << s << ",";
}
cout << endl;
vector<int> v1(ia + 1, ia + 4);
for(auto s : v1){
cout << s << ",";
}
cout << endl;
*/}

c/c++ 学习互助QQ群:877684258

本人微信:xiaoshitou5854

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289