C++ Primer学习笔记(二)
题外话:一工作起来就没有大段的时间学习了,如何充分利用碎片时间是个好问题。
- #include <iostream>
- #include <string>
- using namespace std;
- //char array以'\0'为结束标记,需要手动提供!!!
- int main(){
- int is[]={,,,,};
- cout<<is<<endl;
- // char cs[5]={'a','b','c','d','e','\0'}; //error
- // char cs[5]={'a','b','c','d','e'}; //error
- char cs[]={'a','b','c','d','\0'}; //ok
- cout<<cs<<endl;
- return ;
- }
- int ival = ;
- int *pi = ; // pi initialized to address no object
- int *pi2 = & ival; // pi2 initialized to address of ival
- int *pi3; // ok, but dangerous, pi3 is uninitialized
- pi = pi2; // pi and pi2 address the same object, e.g. ival
- pi2 = ; // pi2 now addresses no object
C++ 语言无法检测指针是否未被初始化,也无法区分有效地址和由指针分配到的存储空间中存放的二进制位形成的地址。
- #include <iostream>
- #include <string>
- using namespace std;
- //练习ref和pointer
- int main(){
- string str="hello world!";
- string &ref=str;
- string *p1=&str;
- string *p2=&ref;
- //引用和原变量指向同一个对象
- cout<<p1<<endl;
- cout<<p2<<endl;
- //修改引用的内容,不是重定向,而是修改!!!
- ref="what\'s this";
- p2=&ref;
- cout<<p2<<endl;
- return ;
- }
- int ia[] = {,,,,};
- int *ip = ia; // ok: ip points to ia[0]
- int *ip2 = ip + ; // ok: ip2 points to ia[4], the last element in ia
- string str="xxx"; //字符串字面值常量
- typedef string *pstr;
- const pstr p=&str; //这里的const pstr p和 pstr const p一样,都是将p 设为const,而非指向const对象。务必注意这点!!!
- #include <iostream>
- #include <string>
- using namespace std;
- int main(){
- int i = -;
- int *p1 = &i;
- const int *p2 = &i;
- int *const p3 = &i;
- const int *const p4 = &i;
- const int i2 = i;
- const int *p5 = &i2;
- // int *const p6 = &i2;//[Error] invalid conversion from 'const int*' to 'int*' [-fpermissive] 。
- // 指向const对象的指针,必须[const type *p]格式 。
- const int *const p7 = &i2;
- return ;
- }
- const char *cp1 = "A string example";
- const char *cp2 = "A different string";
- int i = strcmp(cp1, cp2); // i is positive
- i = strcmp(cp2, cp1); // i is negative
- i = strcmp(cp1, cp1); // i is zero
- 标准库函数 strcmp 有 3 种可能的返回值:
- 若两个字符串相,则返回 0 值;
- 若第一个字符串大于第二个字符串,则返回正数,否则返回负数。
- // 动态数组,内置类型,无初始化
- int *p=new int[];
- while(*p){
- cout<<*p<<endl;//未初始化,所以数值是乱的
- p++;
- }
也可使用跟在数组长度后面的一对空圆括号,对数组元素做值初始化:int *pia = new int[10]();
- string s1("hehe"); // 可以使用字符串字面值常量初始化string
- char *p = "hehe"; //OK
- char *p = &s1; // Error,无法使用string对象初始化字符指针!
上面可以使用string的方法来返回一个指针。
- const char *p = s1.c_str();
- c_str 函数返回 C 风格字符串,其字面意思是:“返回 C 风格字符串的表示方法”,即返回指向字符数组首地址的指针,该数组存放了与 string 对象相同的内容,并且以结束符 null 结束。
- c_str 返回的数组并不保证一定是有效的,接下来对 s1 的操作有可能会改变 s1 的值,使刚才返回的数组失效。如果程序需要持续访问该数据,则应该复制 c_str 函数返回的数组。
31、 C++ 允许使用数组初始化 vector 对象:
- const size_t arr_size = ;
- int int_arr[arr_size] = {, , , , , };
- // ivec has 6 elements: each a copy of the corresponding element in int_arr
- vector<int> ivec(int_arr, int_arr + arr_size); // 相当于给定首、尾指针,含头不含尾!
- const size_t size = ;
- int arr[size] = {,,,,};
- vector<int> ivec(arr, arr+size);
- // cout<<ivec<<endl;
- for(vector<int>::iterator iter=ivec.begin();iter!=ivec.end();iter++){
- cout<<*iter<<endl;
- }
- int *p = arr;
- cout<<*p<<endl;
- int *p1 = (arr+); // 帅呆了
- cout<<*p1<<endl;
- //测试:相当于给定首、尾指针,含头不含尾!
- int *p2; // 未初始化,注意
- vector<int> ivec2(p2, p2+);
- for(vector<int>::iterator iter=ivec2.begin();iter!=ivec2.end();iter++){
- cout<<*iter<<endl;
- }
C++的&&和||是短路求值。short-circut evaluation。(&和|则是位运算符)
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- // string * 的vector,打印string。
- int main(){
- string s1="hehe";
- string s2="haha";
- string s3="what";
- string s4="abc";
- string s5="world";
- vector<string*> pvec;
- pvec.push_back(&s1);
- pvec.push_back(&s2);
- pvec.push_back(&s3);
- pvec.push_back(&s4);
- pvec.push_back(&s5);
- for(vector<string*>::iterator iter=pvec.begin();iter!=pvec.end();++iter){
- cout<<*iter<<"\t"<<**iter<<"\t"<<(*iter)->size()<<endl;
- }
- return ;
- }
- sizeof返回的类型是size_t。
- sizeof (type name);
- sizeof (expr);
- sizeof expr;
- 将 sizeof 用于 expr 时,并没有计算表达式 expr 的值。特别是在 sizeof *p 中,指针 p 可以持有一个无效地址,因为不需要对 p 做解引用操作。
- • 对 char 类型或值为 char 类型的表达式做 sizeof 操作保证得 。
- • 对引用类型做 sizeof 操作将返回存放此引用类型对象所需的内在空间大小。
- • 对指针做 sizeof 操作将返回存放指针所需的内在大小;注意,如果要获取该指针所指向对象的大小,则必须对指针进行引用。
- • 对数组做 sizeof 操作等效于将对其元素类型做 sizeof 操作的结果乘上数组元素的个数。
- 因为 sizeof 返回整个数组在内存中的存储长度,所以用 sizeof 数组的结果除以 sizeof 其元素类型的结果,即可求出数组元素的个数。
- #include <iostream>
- using namespace std;
- int main(){
- int *pi=new int[]; //动态创建对象,只能返回地址
- delete [] pi;
- int *p = new int;
- delete p;
- p=; //防止悬垂指针
- string *pstr=new string(,'');
- cout<<*pstr<<endl;
- delete pstr;
- pstr=; //防止悬垂指针
- return ;
- }
- 如果编译器不提供自动转换,使用 static_cast 来执行类型转换也是很有用的。例如,下面的程序使用 static_cast 找回存放在 void* 指针中的值:
- void* p = &d; // ok: address of any data object can be stored in a void*
- double *dp = static_cast<double*>(p); // ok: converts void* back to the original pointer type
C++ Primer学习笔记(二)的更多相关文章
- C++ Primer学习笔记二
vector<int> a(10,0); for(vector<int>::iterator itor=a.begin();itor!=a.end();itor++) *ito ...
- C++Primer学习笔记(二、基础)
1.两种初始化方式,直接初始化语法更灵活,且效率更高. ); // 直接初始化 direct-initialization ; // 赋值初始化 copy-initialization 2.const ...
- C++ Primer学习笔记(三) C++中函数是一种类型!!!
C++中函数是一种类型!C++中函数是一种类型!C++中函数是一种类型! 函数名就是变量!函数名就是变量!函数名就是变量! (---20160618最新消息,函数名不是变量名...囧) (---201 ...
- WPF的Binding学习笔记(二)
原文: http://www.cnblogs.com/pasoraku/archive/2012/10/25/2738428.htmlWPF的Binding学习笔记(二) 上次学了点点Binding的 ...
- AJax 学习笔记二(onreadystatechange的作用)
AJax 学习笔记二(onreadystatechange的作用) 当发送一个请求后,客户端无法确定什么时候会完成这个请求,所以需要用事件机制来捕获请求的状态XMLHttpRequest对象提供了on ...
- [Firefly引擎][学习笔记二][已完结]卡牌游戏开发模型的设计
源地址:http://bbs.9miao.com/thread-44603-1-1.html 在此补充一下Socket的验证机制:socket登陆验证.会采用session会话超时的机制做心跳接口验证 ...
- JMX学习笔记(二)-Notification
Notification通知,也可理解为消息,有通知,必然有发送通知的广播,JMX这里采用了一种订阅的方式,类似于观察者模式,注册一个观察者到广播里,当有通知时,广播通过调用观察者,逐一通知. 这里写 ...
- java之jvm学习笔记二(类装载器的体系结构)
java的class只在需要的时候才内转载入内存,并由java虚拟机的执行引擎来执行,而执行引擎从总的来说主要的执行方式分为四种, 第一种,一次性解释代码,也就是当字节码转载到内存后,每次需要都会重新 ...
- Java IO学习笔记二
Java IO学习笔记二 流的概念 在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成. 程序中的输入输 ...
随机推荐
- APP三种开发模式
目前主流应用程序大体分为三种:Web App(网页应用).Hybrid Ap(混合应用)p. Native App(原生应用). nativeapp是一个原生程序,一般运行在机器操作系统上,有很强的交 ...
- mysql 查看是否被锁
SHOW OPEN TABLES FROM huahua
- mysql 表锁——读锁和写锁
注意, 0.表的索引类型必须是InnoDB.相关链接:http://www.cnblogs.com/CyLee/p/5579672.html 1.如果你使用Navicat Premium,有可能会出现 ...
- Linux安装mysql——源码安装
1.假设已经有mysql-5.5.10.tar.gz以及cmake-2.8.4.tar.gz两个源文件 (1)先安装cmake(mysql5.5以后是通过cmake来编译的) [root@ rhel5 ...
- object-c 获得目录(包括子目录)下所有文件和文件夹路径
void getAllPathNameInDirectory(vector<string>&filePathList,vector<string>&direct ...
- mybatis 一二事(3) - 多表关联查询
db.properties jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/order jdbc.user ...
- 微信wap开发---页面自适应大小
<meta name="viewport" content="width=device-width, initial-scale=0.5, minimum-scal ...
- Ubuntu12.04下tomcat的安装与配置
1.下载tomcat 我的tomcat是从 http://tomcat.apache.org/download-70.cgi 这里下载的tar.gz版本的. 2.解压tomcat $sudo tar ...
- js修改input的type属性问题(兼容所有浏览器,主要用于密码类的默认有提示文字的效果)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- java的regex问题笔记
参考javadoc java.util.regex.Pattern 里面有一些说明,如果还有不明白的地方 yes,google it. @ “不能以0开头,1到多位数字,字符集为0到9” " ...