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

C++代码复习笔记:第三章

2017-06-21 11:16 357 查看
  getline这个方法的使用, 可以获取用户输入的一行数据, 保存到数组中:

#include <iostream>
#include <string>
int main() {
using namespace std;
// string s;
// getline(cin, s);
// cout << s << endl;
const int size = 4;
string strs[size];
for(int i=0; i<size; i++) {
getline( cin, strs[i] );
}
for(int i=0; i<size; i++) {
cout << strs[i] << endl;
}
return 0;
}


  使用内置类, 计算字符串的长度:

#include <iostream>
#include <cstring>
int main() {
using namespace std;
char str[] = "abcd";
cout << strlen(str) << endl;
return 0;
}


  C++模版类, 和java泛型差不多 , C语言是没有模版类这一说的, C++中新增了模版:

#include <iostream>
#include <string>
#include <array>

int main() {
using namespace std;
const int SIZE = 4;
array<string, SIZE> *p;
for(int i=0; i<SIZE; i++) {
cout << "enter number" << endl;
cin >> (*p)[i];
cout << "" << endl;
}
return 0;
}


  数字指针, 在实际上,如果一个函数返回指针没有啥意义,因为, 如果函数内部要返回指针,那么这个指针必须是外部传递进来的参数,如果是内部创建指针, 当函数执行结束的时候, 内部指针自然会被销毁;

#include <iostream>
#include <string>
#include <array>
int* run(int *n) {
*n = (*n)*100;
return n;
}
int main() {
int number = 100;
int *n = run(&number);
std::cout << *n << std::endl;
return 0;
}


  指针的写法非常多,稍微不同的写法,代表的意义可能天差地别, 以下的(*p)[0]是指获取数组的第一个值 , 但是*p[0]的意思却完全不同了:

std::array<int,4> *p;
(*p)[0] = number;


  函数指针

#include <iostream>
#include <string>
#include <array>
void show(int a, int b) {
using namespace std;
cout << a+b <<endl;
}
int main() {
void (*p)(int, int);
p = show;
(*p)(1,2);
return 0;
}


  函数指针

#include <iostream>
using namespace std;
void show(int num) {
cout << num*10000 << endl;
}
void brige(int num, void (*p)(int) ) {
p(num);
}
int main() {
int number;
cout << "enter number" << endl;
cin >> number;
brige(number, show);
return 0;
}


  内联函数, 是C++新增的特性, 使用的比较少, 主要是代码优化有关系, 对于代码比较少, 而且调用次数比较多的函数, 可以定义为内联函数;

  内部原理是定义宏:

#include <iostream>
#include <string>
inline void show() {
using namespace std;
cout << "nice" << endl;
}
int main() {
show();
return 0;
}


  引用也是C++新增的特性, 务必区别于指针, 引用的地址和原来数据的地址是相同的, 引用必须在变量定义的时候就初始化:

#include <iostream>
int main() {
int i = 1111;
int &j = i;
j = 2222;
std::cout << i << std::endl;
std::cout << &i << std::endl;
std::cout << &j << std::endl;
return 0;
}


  按照引用传递主要用在函数参数中, 在形参上定义引用, 那么在函数内部做的任何数据修改, 都会修改原始值:

#include <iostream>
void swap (int &a, int &b) {
int temp;
temp = a;
a = b;
b = temp;
}
int main() {
using namespace std;
int a = 1;
int b = 2;
swap( a, b );
cout << a << endl; //输出2
cout << b << endl; //输出1
return 0;
}


  编译器会发出警告, 告诉开发者一些错误, 比如函数没有指定返回值, 但是这个并不会影响程序正常运行:

#include <iostream>
#include <cstring>
int run () {
if(false){
return 1;
}else{

}
}
int main() {
run();
printf("end of file");
return 0;
}


  C++中原型的概念:原型描述了函数的接口, 它将函数的返回类型, 参数类型以及参数个数定义在文件头, 原型有利于程序执行效率的进行提高 , 相当于java中的接口文件, 只是包含接口, 没有具体的实现:

#include <iostream>
void run(); //原型的定义
#include <cstring>
int main() {
run();
return 0;
}
void run () {
std::cout << "done" << std::endl;
}


  😅, 常量指针也有一些比较有意思的地方, 比如const声明的位置,const int *p = # int * const p1 = &num, 是完全不同的:

#include <iostream>
#include <cstring>
using namespace std;
int main() {
int num = 100;
const int *p = #
int * const p1 = #
std::cout << *p << std::endl;
std::cout << *p1 << std::endl;
return 0;
}


  const int * p 中, const修饰的是int, 所以只要p的指针指向的类型是数字, 都符合编译器的规范,也就是说, 我们可以改变p的地址;

  int * const p中, const修饰的是p, 所以p这个指针式不能变的, 但是通过*p, 我们可以改变原来数据的值;

#include <iostream>
#include <cstring>
using namespace std;
int main() {
int num = 100;
//常量指针
const int *p = #
//*p = 1; 是错误的, 指针不能变;
int n = 101;
//如果对指针重新赋值是可以的, 因为const是修饰int, 只要p指向的数据类型是int就可以;
p = &n;
std::cout << *p << std::endl;
return 0;
}


#include <iostream>
#include <cstring>
using namespace std;
int main() {
int num = 100;
//常量指针
int * const p = #
*p = 101;
std::cout << *p << std::endl;
return 0;
}


  数组指针加1, 数组指针会自动往下移动一个位:

#include <iostream>
#include <cstring>
using namespace std;
int main() {
int arr[3][2] = {{0,0},{1,1},{2,2}};
printf("%p\n", arr[0]);
printf("%p\n", arr[0]+1);
printf("%p\n", arr+1);
printf("%p\n", arr+2);
return 0;
}


  C的字符串其实就是一堆字符组成的数组, 最后一位以 \0 结束, 所以创建字符串的方法还是比较多的:

#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str[] = "abcd";
char str1[] = {'a','b','c','d','e','\0'};
char *str2 = "abcdef";
printf("%s\n",str);
printf("%s\n",str1);
printf("%s\n",str2);
return 0;
}


  获取字符串的长度:

#include <iostream>
#include <cstring>
using namespace std;
int size(char *p) {
int c = 0;
while(*p) {
c++;
p++;
};
return c;
}
int main() {
char str[] = "abcd";
char str1[] = {'a','b','c','d','e','\0'};
char *str2 = "abcdef";
cout << size(str) << endl;
cout << size(str1) << endl;
cout << size(str2) << endl;
return 0;
}


  函数默认参数:

#include <iostream>
#include <cstring>
using namespace std;
void defFn(int i, int j = 2) {
cout << i + j << endl;
}
int main() {
defFn(1,1);
defFn(1);
return 0;
}


  函数重载:

#include <iostream>
#include <cstring>
using namespace std;
void defFn(int i);
void defFn(double j);
int main() {
defFn(1);
defFn(1.1);
return 0;
}
void defFn(int i) {
cout << "int " << i << endl;
}
void defFn(double j) {
cout << "double " << j << endl;
}


  泛型模版:

#include <iostream>
#include <cstring>
using namespace std;
template <typename T>
void show(T name);
int main() {
show(1234);
show('a');
return 0;
}
template <typename T>
void show(T name) {
cout << name << endl;
}


  其他:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
printf("%d\n", atoi("510"));
printf("%ld\n", atol("5101231231231"));
char str[10] = {'a','b','c','d','\n'};
fgets(str, sizeof(str), stdin);
printf("%s%s\n", str, str);
char name[10];
scanf("%s", name);
printf("%s", strcat(name,"hello"));
int ren = rename("name1", "name");
int ren1 = rename("name", "name1");
int ren2 = rename("name2", "name3");
printf("%d, %d, %d\n",ren, ren1, ren2);
perror(str);
return 0;
}


  EOF

厦门点燃未来网络科技有限公司, 是厦门最好的微信应用, 小程序, 微信网站, 公众号开发公司
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: