- 前言
- 易错代码
- 将text文件中的内容流向控制台
- cin.read() cin.write() cin.gcount()的用法
- cin.peek的用法
- cin.ignore的用法
- cin>>ch和cin.get(ch)的比较
- cin.get()接受空格
- substr
- 计算输出的字符的个数
- 循环输出数组中的内容
- ++和*的优先级
- ++
- for循环字符串逆序
- 在for循环中初始化初始值
- bool
- 输入一行 中间可以空格
- 计算string字符串长度
- new
- cout.put输出一个字符
- 猜数字
- string字符串直接相加
前言 易错代码
#includeusing namespace std; int main() { int i = 0; int k = 0; for (i = 0, k = 0; k = 0; i++, k++) k++;//循环一次也不执行 k=0 为假 不进入循环 return 0; }
#includeusing namespace std; int main() { int n, x; cin >> n >> x; int count = 0; for (int i = 1; i <= n; i++) { while (i) { if (i % 10 == x) count++; i /= 10; } } //这种方法是有错误的 //i/10 =0; while循环结束 //i++ 后i等于1 陷入死循环 cout << count << endl; return 0; }
#includeusing namespace std; int main() { int x = 2; int y = 2; y = x++ + 1;//x=2先参与运算,然后x在加1 cout << y << endl; cout << x << endl; return 0; }
#includeusing namespace std; int main() { int x = 5; cout << x++ << endl;//5 x先使用 在加加 cout << x << endl;//6 return 0; }
#includeusing namespace std; int main() { int x = 5; if (x-- < 5)//x=5先判断 5<5 为假 x参与运算玩后减1 { cout << x << endl; } else//进入else 此时x=4 cout << x++ << endl;//4 x=4 先参与运算 运算玩后在加1 cout << x << endl;//5 return 0; }
#includevoid arrayz(int array[]) { printf("array:%dn", sizeof(array));//这里的array是指针。占四个字节 } int main() { int data[] = { 0,1,2,3,4,5,6,7,8,9 }; arrayz(data); printf("data:%dn", sizeof(data));//这里的sizeof(data)计算的是数组中所有元素在 //内存中所占空间的大小 占四十个字节 return 0; }
int a, b;
void fun()
{
a = 100;
b = 200;
}
int main()
{
int a = 5, b = 7;
fun();
printf("%d %dn", a, b);//输出5 和 7
}
int f(int a)
{
int b = 0;
static int c = 3;
b++; c++;
return (a + b + c);
}
int main()
{
int a = 2, i;
for (i = 0; i < 3; i++)
printf("%dn",f(a));//输出7 8 9
}
将text文件中的内容流向控制台
#includecin.read() cin.write() cin.gcount()的用法#include using namespace std; int main() { ifstream in; in.open("test.txt"); if (!in) { cerr << "打开文件失败" << endl; return 0; } char x; while (in >> x) { cout << x; } cout << endl; in.close(); return 0; }
#includecin.peek的用法using namespace std; int main() { const int size = 50; char buf[size]; cout << "请输入一段文本" ; cin.read(buf, 20);//输入一段字符串 cout << "字符串收集到的字符数是:" << cin.gcount() << endl; //统计输入了多少字符串 cout << "输入的文本信息是:" ; cout.write(buf, 20);//输出一段字符串 cout << endl; return 0; }
#includecin.ignore的用法using namespace std; int main() { char p; cout << "请输入一段文本" << endl; while (cin.peek() != 'n')//从输入中提取一个字符判断是不是‘n’ { p = cin.get();//提取字符并赋给p cout << p; } return 0; }
#includecin>>ch和cin.get(ch)的比较using namespace std; int main() { char buf[20]; cin.ignore(7);//输出忽略前7个字符 cin.getline(buf, 10);//输入一行字符 并输出前十个字符 cout << buf << endl; return 0; }
cin>>ch输入字符时将忽略空格和换行符。
cin.get(ch)可输入任意字符并接受。
#includesubstrusing namespace std; int main() { int year; char arr[20]; cin >> year; cin.get(); cin.getline(arr, 20);//再输入year是会按下一个回车键 回车键被这一行代码读取,所以就没有输出 //此时需要cin.get()来接收这个空格 cout << year << endl; cout << arr << endl; return 0; }
#include#include using namespace std; int main() { string s = "Hello world!"; string s_sub = s.substr(10, 1);//从字符串的第十个字符处开始往后打印 并往后打印1个字符 cout << s << endl; cout << s_sub << endl;//输出d return 0; } #include #include using namespace std; int main() { string s = "Hello world!"; string s_sub = s.substr(6);//从字符串的第6个字符处开始往后打印至结束 cout << s << endl; cout << s_sub << endl;//输出world return 0; }
#include计算输出的字符的个数using namespace std; #include int main() { int guests = 0; while (guests++ < 10) { cout << guests << endl;//输出1 2 3 4 5 6 7 8 9 10 } return 0; }
#include循环输出数组中的内容using namespace std; int main() { int ch; int count = 0; while ((ch = cin.get()) != EOF) { cout.put(char(ch)); ++count; } cout << endl << count << " characters read" << endl; return 0; }
#include++和*的优先级using namespace std; int main() { double prices[5] = { 1.2,23.5,1.2,5.5,4.5 }; for (double x : prices) { cout << x << endl;//循环输出数组中的内容 } return 0; }
#includeusing namespace std; int main() { double arr[] = { 21.1,23.8,23.4,45.2,37.4,22.2 }; double* pt = arr;//pt指向数组的首元素 ++pt;//pt指向数组的第二个元素 double x = *++pt;//pt先指向数组的第三个元素,在解引用 答案是23,4; cout << x << endl; x = ++ * pt; cout << x << endl;//pt指向数组的第三个元素 先解引用,得到23.4 在进行++ 答案是24,4; x = (*pt)++; cout << x << endl;//pt指向数组的第三个元素 先解引用,得到24.4 ,因为是后置++,先赋值 x=24.4, //在++ 但此时答案是24.4 进入到下一个语句中 是25.4 x = *(pt)++; cout << x << endl;//pt先解引用 得到25.4 在指针++ 指向第四个元素的地址 x = *pt++; cout << x << endl;//++的优先级比*高 ,因此指针先递增, //指向第四个元素,但是++为后置++,还是对第四个元素的地址进行解引用 //但该语句执行完毕后,pt的值是第五个元素的地址 答案是45.2 x = *(pt++);/ //cout << a << endl; return 0; }
getline(cin,str)是用于字符串输入一行且中间可以有空格
cin.getline(arr,20)是用于数组形式,getline函数每次读取一行,他通过换行符来确定行尾,但不保存换行符。
cin.get(arr,20)也用于数组,但他保存了换行符。
所以需要cin.get()来接收这个换行符
cin.get(arr,20);
cin.get();//合并这两句cin.get(arr,20).get();
cin.get(arr2,20);
#include计算string字符串长度using namespace std; int main() { int year; cin>>year; cin.get();//(cin>>year).get();也可以用这句,需要接受掉确认输出的那个换行符 char arr[20]; cin.getline(arr, 20); cout << year << endl; cout << arr << endl; return 0; }
#includenew#include using namespace std; int main() { string a; getline(cin, a); cout << a.size() << endl; cout << a.length() << endl; return 0; }
#includeusing namespace std; #include int main() { int* p = new int; *p = 9; cout << *p << endl; delete p; return 0; }
#includeusing namespace std; #include int main() { int* p = new int[10]; for (int i = 0; i < 10; i++) { *(p+i) = i; } for (int j = 0;j < 10; j++) { cout << *(p + j) << endl; } delete[]p; return 0; }
#includeusing namespace std; #include int main() { int size = 0; cin >> size; int* p = new int[size]; delete[]p; return 0; }
#includecout.put输出一个字符using namespace std; #include struct people { char name[20]; int age; double score; }; int main() { people* ps = new people; cin.get(ps->name, 20); cin >> (*ps).age; cin >> ps->score; cout << ps->name << ps->age << (*ps).score << endl; delete ps; return 0; }
#include猜数字using namespace std; #include int main() { cout.put('c'); return 0; }
#includestring字符串直接相加using namespace std; #include int main() { srand((unsigned int)time(NULL)); int num = rand() % 100 + 1; int a = 0;//玩家猜的数 int max = 10;//设置最多可以猜10次 cout << "请输入一个1~100的整数:"; cin >> a; while (max > 0)//(a=num)猜对了跳出循环,max<0也跳出循环,10次机会用完,这种是失败了 { max--; if (a > num) { cout << "猜大了,您还剩" << max << "次机会" << endl; cout << "重新输入:"; cin >> a; } else if (a < num) { cout << "猜小了,您还剩" << max << "次机会" << endl; cout << "重新输入:"; cin >> a; } else { cout << "猜对了,游戏胜利" << endl; break; } } if (max < 0 || a != num) { cout << "很遗憾您失败了,这个数是" << num << endl; } return 0; }
#includeusing namespace std; #include int main() { string a = "I"; string b = "you"; cout << a + b << endl; return 0; }
#includeusing namespace std; #include int main() { string a = "abc"; string b = a; cout << b << endl; return 0; }



