CPP Primer 第四章
来源:互联网 发布:mac登不上app store 编辑:程序博客网 时间:2024/05/22 07:06
//////////////////////////////////////////////////////////////////////////
//数组
//
/*
void main()
{
const unsigned buf_size= 512, max_files = 20;
int staff_size = 27;
const unsigned array_size = 3;
int ia[array_size] = {0,1,2};
string str_arr[array_size] = {"hi","bye"};
//字符数组既可以用一组由花括号括起来、逗号隔开的字符字面值进行初始化,
//也可以用一个字符串字面值进行初始化。
//但是,字符串字面值自动包含一个空字符用于结束字符串。
char ca1[] = {'C','+','+'};
char ca2[] = {'C','+','+','/0'};
char ca3[] = "C++";//ca2 和 ca3 一样,维数是4
//数组不允许直接复制和赋值。
}
*/
/*
void main()
{
const size_t array_size = 10;
int ia[array_size];
for (size_t ix = 0; ix != array_size; ++ ix)
{
ia[ix] = ix;
}
}*/
//////////////////////////////////////////////////////////////////////////
//4.2 指针
//5-25
//vector的遍历可使用下标或迭代器,
//数组的遍历可用下标或指针
/*
#include <string>
#include <vector>
using namespace std;
void main()
{
string s("hello world");
string *sp = &s;
vector<int> ivec ;
vector<int> *pvec = &ivec;
int ival = 1024;
int *pi = 0;
int *pi2 = &ival;
int *pi3 = 0; //指针可以用0初始化,表示不指向任何值。
pi = pi2;
pi2 = 0;
string *sp1 = 0;
//除了使用数值0或在编译时值为0的const量外,还可以使用C++语言从C语言中继承下来的预
//处理变量NULL,值为0
int *p = NULL;
//void* 指针
//它可以保存任何类型对象的地址。
double obj = 3.14;
double *pd = &obj;
void *pv = &obj;
pv = pd;
//int *pi1 = pv;
//cannot convert from 'void *' to 'int *'
//但不允许使用void*指针操纵它所指向的对象。因为它不知道它所指向的对象的类型。
}
*/
/*
#include <string>
#include <vector>
#include <iostream>
using namespace std;
void main()
{
string s("hello world");
string *sp = &s;
cout << *sp << endl;
*sp = "goodbye";
cout << *sp << endl;
string s2 = "some value";
sp = &s2;
cout << *sp << endl;
string *sp3 = 0;
*sp3 = "goodbye"; //这样行 //表示sp3指向的值 //这难道是在堆中建个字符串,由指针指向它?
//string *sp3 = "goodbye"; //这样不行 //表示*sp3要地址初始化
}*/
//4.2.4使用指针访问数组元素
/*
#include <string>
#include <vector>
#include <iostream>
using namespace std;
void main()
{
int ia[] = {0,2,4,6,8};
int *ip = ia;
int *ip2 = ip+ 4;
//ip = &ia[4];
ptrdiff_t n = ip2 - ip;
cout << n << endl;
}
*/
/*
#include <string>
#include <vector>
#include <iostream>
using namespace std;
void main()
{
int ia[] = {0,2,4,6,8};
int i = ia[0];
int *p = &ia[2];
int j = p[1];
int k = p[-2];
cout << i << *p << j << k << endl;
const size_t arr_sz = 5;
int int_arr[arr_sz] = {0,1,2,3,4};
for(int *pbegin = int_arr, *pend = int_arr + arr_sz; pbegin != pend; ++pbegin)
{
cout << *pbegin << ' ';
}
}
*/
//4.2.5指针和const限定符
/*
void main()
{
//指向const对象的指针和const指针
//指向const对象的指 针
const double *cptr1 = 0;
//可以指向其它const对象,但不能通过这个指针修改其所指向的值。
//const指针
int errNumb = 0;
int *const curErr = &errNumb;
//指针不能指向其它对象,如果其指向的对象不是const的,那么可以通过指针修改其所指向的对象。
const double pi = 3.14;
//double *ptr = π//cannot convert from 'const double *' to 'double *'
const double *cptr = π
//不能使用void* 保存const对象的地址,而必须使用const void* 类型的指针保存const对象的地址。
// typedef string *pstring;
// const pstring cstr; // const string * cstr | const * string cstr ?
//int *a;
}
*/
//4.3C风格字符串
#include <string>
#include <cstring> // string.h的C++版本
#include <iostream>
using namespace std;
void main()
{
const char *cp ="some value";
// char ca[] = {'C','+','+'};
// cout << strlen(ca) << endl; //disaster: ca isn't null-terminated
}
//字符串字面值的类型就是const char类型的数组。
//C风格字符串:以空字符null结束的字符数组。
//4.3.1创建动态数组
//在自由存储区或堆
//C语言使用一对标准库函数malloc和free
//C++用new 和 delete
//初始化动态分配的数组
//--如果是类类型,将使用该类的默认构造函数。
//--如果是数组元素,则无初始化。
//string *psa = new string[10];//初始化为空字符串
//int *pia = new int[10]; //没有初始化
//可使用跟在数组长度后面的一对空圆括号,对数组做值初始化。
//int *pia2 = new int[10](); //初始化了
//对于动态分配的数组,其元素只能初始化为元素类型的默认值,而不能像数组变量一样,
//用初始化列表为数组元素提供各不相同的初值。
//3 const对象的动态数组
const int *pci_ok = new const int[100](); //用处不大,因为初始化后不能修改了
const string *pcs = new const string[100];
//动态空间的释放。
//delete [] pia;
string st2;
const char *str = st2.c_str();
//使用数组初始化vector对象
const size_t arr_size = 6;
int int_arr[arr_size] = {0,1,2,3,4,5};
vector<int> ivec(int_arr, int_arr+arr_size);
- CPP Primer 第四章
- CPP Primer 第十五章
- CPP Primer 第五章
- C++ primer 第四章
- C++ Primer 第四章
- C++ primer第四章
- C++Primer第四章
- C++primer 第四章
- c++ primer 第四章
- C++ Primer Plus 第四章
- C++ Primer Plus 第四章
- C++primer plus第四章
- C Primer plus 第四章
- C++ primer | 第四章 表达式
- c++primer第四章 表达式
- C Primer Plus 第四章
- C++ Primer 第四章 表达式
- C++ Primer 第四章 表达式
- Session和Cookie的深入研究
- datalist的数据导入到Excel中
- PHP开发者:你GLAMMP了吗?
- 开源等于省钱?开发者们意见纷纷
- google maps api
- CPP Primer 第四章
- centos 4.3 iptable配置文件
- 在NS中添加一个新协议的步骤
- buffer cache深度分析:概念以及内存结构
- VC++下命名管道编程的原理及实现
- 图片切换效果
- Hibernate 使用说明
- 中国最火最流行的40个博客网站
- cannot create windows service for mysql. error 0—mysql启动错误1067的解决