您的位置:首页 > 编程语言 > C语言/C++

c++Primer笔记1(快速入门、变量和基本类型、标准库类型)

2014-06-01 15:47 344 查看

C++快速入门

在CSDN上看到有位同学把自己的学习笔记全部记录下来,感觉挺好的,一直也想做这么一件事,

看自己是否能够坚持下来,坚持记录每次的学习笔记。

一、简单C++程序

int main(int argc, char* argv[])
{
printf("Hello World!\n");
return 0;
}
呵呵。每种语言,每本入门书,永远是Hello World!

第二章 变量和基本类型
一、声明和定义:

1. 变量的定义用于为变量分配存储空间,还可以为变量指定初始值。在一个程序中,一个变量有且仅有一个定义。

2. 声明用于向程序表明变量的类型和名字,不分配存储空间。定义也是声明:当定义变量时我们声明了它的类型和名字。可以通过使用extern关键字声明变量而不是定义它。

Extern表明变量的定义在程序的其他地方。

只有当Extern声明位于函数外部时,才可以含有初始化式。

二、const限定符

1.const修饰谁,谁就在定义之后不能被修改了。

2.const对象默认为文件的局部对象。即该变量只存在于定义的文件中,不能被其他文件访问。

3.指定const变量为extern,就可以在整个程序中访问const对象。

int ival = 1.01;
int &rvall = ival;
int &rval2 = ival;
const int &rval3 = 1;

rval2 = 3.14159;
rval2 = rval3;
ival = rval3;
//rval3 = ival;

int ival2 = 0;
const int &ri = 0;

ival2 = ri;
//ri = ival2;//err

int i=0,&rii=i;
i=5;
rii=10;
std::cout<< i << " " << rii << std::endl;


三、引用

1.引用是别名。当引用初始化之后,只要该引用存在,它就保持绑定到初始化时指向的对象,不可能将引用绑定到另一个对象上,这也就是说,引用必须在定义时初始化。

Const引用:指向const对象的引用

非Const引用:指向非const类型的引用

例:

Const int ival = 1024;

Const int &refVal = ival;//const引用

2.如果const变量不是用常量表达式初始化,那么就应该在源文件中定义并初始化,然后在头文件中为它添加extern声明,以使其能被多个文件共享。

//file1.cc
//defines and initializes a const that is accessible to other files
extern const int bufsize = fcn();

//file2.cc
extern const int bufsize;//uses bufsize from file1

//uses bufsize defined in file1
for (int index=0; index < bufsize; ++index)
{
//.........
}


四、避免多重包含

#define 指示接受一个名字并定义该名字为预处理器变量

#ifndef 指示检测制定的预处理器变量是否未定义

可以使用如下格式来预防多次包含同一头文件:

#ifndef AAAAA

#define AAAAA

//something else

#endif

第三章 标准库类型
1.标准库string类型

<span style="font-size:14px;">#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::cin;
using std::endl;
//读未知的字符串并输出
int main()
{
string word;//默认构造一个空的字符串变量

while(cin >> word)
cout << word << endl;

return 0;
}</span>


//习题3.5

string m_line;
while(getline(cin,m_line))
cout << m_line <<endl;

string m_word;
while(cin >> m_word)
cout << m_word << endl;


习题3.6 解释string类型的输入操作符和getline函数分别如何处理空白字符。

答:string类型的输入操作符对空白字符的处理:读取并忽略开头所有的空白字符,直到再次遇到空白字符,读取终止。

getline函数对空白字符的处理:不忽略行开头的换行符,读取字符知道遇到换行符,读取终止并丢弃换行符。

//习题3.7
string s1,s2;

cout << "please input two string:" << endl;
cin >> s1;
cin >> s2;

if (s2 == s1)
{
cout << "they are equal" << endl;
}
else if (s2 > s1)
{
cout << "the big one is s2:" << s2 << endl;
}
else
{
cout << "the small one is s1:" << s1 << endl;
}

if (s1.size()== s2.size())
{
cout << "their size are equal" << endl;
}
else if (s1.size() < s2.size())
{
cout << "the long one is s2:" << s2 << endl;
}
else
{
cout << "the long one is s1:" << s1 << endl;
}


//习题3.8
string bigstr,shortstr;
while (cin >> shortstr)
{
if (!bigstr.empty())
{
bigstr += " ";//在相邻的string后面添加空格
}
bigstr += shortstr;
}
cout << bigstr << endl;


//习题3.9
string s;
cout << s[0] << endl;
//合法,输出空格,因为在定义s的时候,会调用string的默认构造函数,将s赋值为空串


//习题3.10
string s;
cin >> s;
string::size_type punct_cnt=0;
for (string::size_type index=0;index != s.size();++index)
{
if (!ispunct(s[index]))
cout << s[index];
}
cout << endl;


2.标准库vector类型

#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;//因为编译器的关系,在这里不能用using std::vector;
using std::cout;
using std::cin;
using std::endl;

int main(int argc, char* argv[])
{
//习题3.13
vector<int> ivec;
for ( int ix=0;ix != 11;++ix)
{
ivec.push_back(ix*3+1);
}
cout << "size of ivec is:" << ivec.size() <<endl;

for (vector<int>::size_type iy = 0; iy != ivec.size(); ++iy)
{
cout << ivec.at(iy)<<endl;
}

//相邻的两个元素相加
for (vector<int>::size_type iz = 0; iz != ivec.size(); ++iz)
{
if ((++ iz) != ivec.size())
{
cout << "ivec[" << (iz-1) << "] plus ivec[" << iz << "] result is " << ivec[iz-1] + ivec[iz] << endl;
}
else
{
cout << "the last one ivec["<< (iz-1) <<"]:"<<ivec[iz-1]<<" have no other element to plus to " << endl;
--iz;
}
}

//头尾两个元素相加
for (vector<int>::size_type ik = 0,il=ivec.size()-1; ik != ivec.size()/2+1 && il  != ivec.size()/2-1; ++ik,--il)
{
if (ik == il)
{
cout << "the last one ivec["<< ik <<"]:"<<ivec[ik]<<" have no other element to plus to " << endl;
}
else
{
cout << "ivec[" << ik << "] plus ivec[" << il << "] result is " << ivec[ik] + ivec[il] << endl;
}
}
return 0;
}


//习题3.14
string wordstr;
vector<string> textstr;
while(cin >> wordstr)
{
//cin >> wordstr;
for (vector<string>::size_type ix=0;ix != wordstr.size();++ix)
{
wordstr[ix] = toupper(wordstr.at(ix));
}
textstr.push_back(wordstr);
}

for (vector<string>::size_type ik=0;ik != textstr.size();++ik)
{
cout << textstr.at(ik);
}
cout << endl;

for (vector<string>::size_type iy=0;iy != textstr.size();++iy)
{
for (int ik=0,il=0;il != textstr.at(iy).size();)
{
if (ik == 8)
{
ik=0;
cout << endl;
}
else
{
cout << textstr.at(iy).at(il);
++ ik;
++il;
}
}
}
cout << endl;


//习题3.15
vector<int> ivec;
//	ivec[0] = 42;//对vector必须用push_back才能插入数据
ivec.push_back(45);
cout << ivec[0] << endl;


//习题3.16
vector<int> ivec1;
vector<int> ivec2(10);
vector<int> ivec3(10,42);

for (int ix=0;ix < 10;++ix)
{
ivec1.push_back(42);
}


//习题3.17   改3.13
vector<int> ivec;
for ( int ix=0;ix != 11;++ix)
{
ivec.push_back(ix*3+1);
}
cout << "size of ivec is:" << ivec.size() <<endl;

for (vector<int>::iterator iy = ivec.begin(); iy != ivec.end(); ++iy)
{
cout << *iy <<endl;
}

//相邻的两个元素相加
vector<int>::size_type ik=0;
for (vector<int>::iterator iz = ivec.begin(); iz != ivec.end(); ++iz,++ik)
{
if ((++ iz) != ivec.end())
{
++ik;
cout << "ivec[" << (ik-1) << "] plus ivec[" << ik << "] result is " << *(iz-1) + *iz << endl;
}
else
{
cout << "the last one ivec["<< ik <<"]:"<<*(iz-1)<<" have no other element to plus to " << endl;
--iz;
}
}

//头尾两个元素相加
ik = 0;
vector<int>::size_type il=ivec.size()-1;
for (vector<int>::iterator  im=ivec.begin(),in=ivec.end()-1; ik != ivec.size()/2+1 && il  != ivec.size()/2-1; ++ik,--il,++im,--in)
{
if (ik == il)
{
cout << "the last one ivec["<< ik <<"]:"<< *in <<" have no other element to plus to " << endl;
}
else
{
cout << "ivec[" << ik << "] plus ivec[" << il << "] result is " << *im + *in << endl;
}
}


//3.18  3.19
vector<int> ivec;
for ( int ix=0;ix != 10;++ix)
{
ivec.push_back(ix*3+1);
}
cout << "size of ivec is:" << ivec.size() <<endl;

for (vector<int>::iterator iy = ivec.begin(); iy != ivec.end(); ++iy)
{
cout << *iy <<endl;
*iy = (*iy)*2;
cout << *iy <<endl;
}


3.21 借用书上的话,const迭代器基本上是没用的,因为要在定义的时候初始化,只能用它来改写其指向的的元素,不能使它指向任何其他元素。

const_iterator类型,只能用于读取容器内元素,不能改变元素的值。

//3.22
vector<int>::iterator mid = (ivec.begin() + ivec.end())/2;

ivec.begin() 不能与ivec.end()相加!


3.标准库bitset类型

//3.32
bitset<64> bitvec7(32);
cout << "bitvec7:" << bitvec7 << endl;
bitset<32> bitvec8(1010101);
cout << "bitve8:" << bitvec8 << endl;
string str1;
cin >> str1;
bitset<8> bitvec9(str1);
cout << "bitvec9:" << bitvec9 << endl;




//3.24
bitset<32> bitvec1(00000000001000000010000010010111);
bitset<32> bitvec2;
bitvec2[0].flip();
bitvec2.flip(1);
bitvec2.flip(2);
bitvec2.flip(4);
bitvec2.flip(7);
bitvec2.flip(12);
bitvec2.flip(20);
cout << "bitvec1:" << bitvec1 << endl;
cout << "bitvec2:" << bitvec2 << endl;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: