c++练习代码
输入两个整数,输出这两个整数范围内的所有整数:
#include "iostream"void find(int &a,int &b)
{if(a < b){int temp;temp = a;a = b;b = temp;}
}int main()
{int v1=0,v2=0;std::cin >> v1 >> v2;find(v1,v2);for(int i = v1 - v2;i >= 0 ;--i){std::cout << v2+i << std::endl;}return 0;}
读取数量不定的输入数据:
#include "iostream"int main()
{int value=0,sum=0;while(std::cin >> value)//输入流无效的时候才会推出循环,测试的时候注意在输入数据完成后接无效输入数据作为截止 {sum += value;}std::cout << "sum的值= " << sum << std::endl;return 0;}
统计输入的数字重复的次数:
#include "iostream"int main()
{//输入的数据有几个是重复的int curren=0,cnt=0;//用来存放输入的数据 if(std::cin>>curren){int cnt = 1,val=0;//更改cntwhile(std::cin>>val){if(val==curren)++cnt;else{std::cout << curren <<" occures "<< cnt << " times " << endl;curren = val;cnt = 1;}}std::cout << curren <<" occures "<< cnt << " times " << endl;}}
指针和类型别名
#include <iostream>
using namespace std;int main()
{typedef int *p;//注意这里p是类型名,我之前就把p当成指针变量来用了 int i = 10;p p1 = &i;cout << *p1 << endl;
}
//这段代码运行会报错
#include <iostream>
using namespace std;int main()
{const int i = 12,&r = i;auto a = i;//auto会丢弃const,所以a是int类型变量,可以被赋值decltype(i) b = 10;//decltype会把i的类型给b,所以b是const int类型的常量,初始化之后不能被赋值,所以后续的赋值操作会报错a = 20;b = 1;
}
定义数据结构并赋类内初始值
#include <iostream>
#include <string>using namespace std;int main()
{struct test{int age{10};//定义数据类型的同时初始化string name{"liming"};int high{180};}; test a;cout << "age:" << a.age << " "<< "name:" << a.name << " "<< "high:" << a.high <<endl;
}
第一个结构体程序
#include <iostream>
#include <string>using namespace std;struct Salaes_date{string bookNO;double units_sold;double revenue;
};int main()
{Salaes_date data1,data2;double price = 0;cin >> data1.bookNO >> data1.units_sold >> price;data1.revenue = data1.units_sold * price;cin >> data2.bookNO >> data2.units_sold >> price;data2.revenue = data2.units_sold * price;if(data1.bookNO == data2.bookNO){unsigned totalCnt = data1.units_sold + data2.units_sold;double totalRevenue = data1.revenue + data2.revenue;cout << data1.bookNO << " " << totalCnt << " " << totalRevenue << " ";if (totalCnt != 0)cout << totalRevenue/totalCnt << endl;elsecout << "(no sales)" << endl;return 0;}else{cerr << "Data must refer to the same ISBN"<< endl;return -1;}return 0;
找出最大的字符串:
#include <iostream>
#include <string>using namespace std;string find(string s1,string s2)
{if(s1 > s2)return s1;elsereturn s2;
}int main()
{string s1 = "hello";string s2 = "hello world";//相同情况下,短的小string s3 = "hiya";//不同情况下,第一个相异字符比较来确定大小//小写字母大,字母顺序,越往后越大cout << find(s3,find(s1,s2)) << endl;//string max = find(string s3,find(s1,s2));
}
读入两个字符串,判断大小:
#include <iostream>
#include <string>using namespace std;int main()
{string s1;string s2;getline(cin,s1);getline(cin,s2);if (s1.size() == s2.size())cout << "s1 = s2" << endl;else if(s1.size() > s2.size())cout << "s1 long" << endl;elsecout << "s2 long" << endl;
}
使用范围for判断string对象中有多好个空格:
#include <iostream>
#include <string>using namespace std;int main()
{int cnt = 0;string s1("some str in g");for(auto c: s1){if (c == ' ')cnt += 1;}cout << cnt << endl;
}
计算字符串中有多少个标点符号:
#include <iostream>
#include <string>using namespace std;int main()
{string s1("hello world!!!");decltype(s1.size()) cnt = 0;for(auto c:s1){if (ispunct(c))//ispunct是cctype中的一个函数,如果是标点符号则为真cnt ++;}cout << cnt << " punctuation characters in " << s1 << endl;
}
字符串变转大写,或小写
#include <iostream>
#include <string>using namespace std;int main()
{string s1("hello world!!!");for(auto &c : s1){c = toupper(c);}cout << s1 << endl;for(auto &c : s1){c = tolower(c);}cout << s1 << endl;
}
把第一个单词转成大写:
#include <iostream>
#include <string>using namespace std;int main()
{string s1("hello world!!!");for(decltype(s1.size()) index = 0;index < s1.size() && !isspace(s1[index]);index++)//到空格结束循环{s1[index] = toupper(s1[index]);}cout << s1 << endl;
}
把所有单词首字母变成大写:
#include <iostream>
#include <string>using namespace std;int main()
{string s1("hello world!!! is me");s1[0] = toupper(s1[0]);for(decltype(s1.size()) index = 0;index < s1.size();index++){if (isspace(s1[index]))//定位空格,把空格后面的字符变成大写{s1[index + 1] = toupper(s1[index + 1]); }}cout << s1 << endl;
}
十进制转16进制:
#include <iostream>
#include <string>using namespace std;int main()
{const string hexdigits = "0123456789ABCDEF";//利用下标,下标1对应的值也正好是1,因此只要把输入的对应的值取出来就是了string result;string::size_type n;while(cin >> n){if (n < hexdigits.size()){result += hexdigits[n];cout << hexdigits[n] << endl;}else{cerr << "enter number error.please reenter" << endl;}}cout << result << endl;
}
把输入的变成大写在存入vector中:
#include <iostream>
#include <string>
#include <vector>//多余
using std::vector;using namespace std;int main()
{string word;vector<string> a;while(cin >> word){//把输入的string对象变成大写for(auto &j : word)j = toupper(j);a.push_back(word);//变成大写的string对象存入vector中}for(auto i : a)//输入vector中的内容cout << i << endl;return 0;
}
读入一组数据存入vector中,将没对相邻的数据和输出:
#include <iostream>
#include <string>
#include <vector>using std::vector;using namespace std;int main()
{int a = 0,temp = 0;vector<int> ivec;while(cin >> a){ivec.push_back(a);}for(decltype(ivec.size()) n = 0;n < ivec.size()-1;n++){cout << ivec[n] + ivec[n + 1] << ' ';}cout << endl;
}
输出头尾元素的和:
#include <iostream>
#include <string>
#include <vector>using std::vector;using namespace std;int main()
{int a = 0,temp = 0;vector<int> ivec;while(cin >> a){ivec.push_back(a);}//右边的下标,不能比左边的下标小for(decltype(ivec.size()) n = 0;n < ivec.size()-n;n++){int tal = ivec.size() - 1;//如果下标相等,则不想加if ((tal - n)==n){cout << ivec[n];break;} cout << ivec[n] + ivec[tal - n] << ' ';}cout << endl;
}
二分搜索,搜索有序数列:
#include <iostream>
#include <string>
#include <vector>using std::vector;using namespace std;int main()
{int sought = 0;cin >> sought;if (sought > 9){cerr << "please enter the right number" << endl;return -1;}vector<int> test{1,2,3,4,5,6,7,8,9};auto beg = test.begin(),end = test.end();auto mid = test.begin() + (end - beg)/2;while(mid != end && *mid != sought){if (sought < *mid)end = mid;elsebeg = mid + 1;mid = beg + (end - beg)/2;}cout << *mid << endl;
}
数组指针:
#include <iostream>
#include <string>
#include <vector>using namespace std;int main()
{//初始化数组int ia[10]={};//ia2是一个int *型变量auto ia2(ia);//ib是一个和ia一样长度的数组decltype(ia) ib;ia2 = &ia[1];*ia2 = 3;for(auto c : ib)cout << c << ' ';cout << endl;
判断数组是否相等:
#include <iostream>
#include <string>
#include <vector>
using namespace std;int main()
{constexpr size_t sz = 5;int a[sz]={1,2,3,4,5},b[6]={1,2,3,4,5};int la = end(a) - begin(a),lb = end(b) - begin(b);if (la != lb){cout << "a!=b" << endl;return -1;}else{for(int i = 0;i < la;i++){if (a[i]!=b[i]){cout << "a!=b" << endl;return -1;}}cout << "a=b" << endl;}
}
vector实现上诉问题:
#include <iostream>
#include <string>
#include <vector>using namespace std;int main()
{vector<int> a{1,2,3,4,5},b{1,2,3,4,5};if (a.size() != b.size()){cout << "a!=b" << endl;return -1;}else{for(int i = 0;i < a.size();i++){if (a[i]!=b[i]){cout << "a!=b" << endl;return -1;}}cout << "a=b" << endl;}
}
c++与c风格的字符串比较:
#include <iostream>
#include <string>
#include <vector>using namespace std;int main()
{//c++的string类型的字符串可以直接比较string s = "hello world";string s1 = "c++ dmeo";//c风格字符串,比较的时候,是比较指针而非字符串char a[] = "aello";char b[] = "bbuntu";//想要比较两个c风格的字符串需要调用strcmp函数if (strcmp(a,b)>0)cout << "a>b" << endl;elsecout << "b>a" << endl;
}
c++和c风格字符串连接:
#include <iostream>
#include <string>
#include <vector>using namespace std;int main()
{//c++的string类型的字符串可以直接比较string s = "hello world";string s1 = "c++ dmeo";//下面这段代码放在最后面就运行不了,无法输出,不知道为什么cout << s + " " + s1 << endl;//c风格字符串,比较的时候,是比较指针而非字符串char a[] = "hello";char b[] = "ubuntu";//想要比较两个c风格的字符串需要调用strcmp函数strcat(a," ");strcat(a,b);cout << a << endl;//c风格中,拷贝字符串时空间比较难以估算,这会导致危险
}
把c字符串赋值给string,通过string对象返回c类型字符串指针:
#include <iostream>
#include <string>
#include <vector>using namespace std;int main()
{string s = "hello world";char sa[] = "demo ubuntu";s = sa;cout << s << endl;const char *p = s.c_str();//c_str返回一个c风格字符串cout << *(p+5) << endl;
}
字符串拷贝:有一个字符串的指针,有目的字符串的名称,strcpy拷贝字符串
#include <iostream>
#include <string>
#include <vector>using namespace std;int main()
{string s = "hello world";const char *p = s.c_str();char sa[12] = {};strcpy(sa,p);cout << sa << endl;
}
数组引用传递:注意,数组长度和引用的长度要一致:
#include <iostream>
#include <string>
#include <vector>using namespace std;
//接收的引用的数组长度要和传入的数组长度一致,且不能留空,此处为5
void print(int (&r)[5])
{for(auto c : r)cout << c;cout << endl;
}int main()
{int a[] = {1,2,3,4,5};print(a);return 0;
}
对传入main函数的参数进行处理:
#include <iostream>
#include <string>
#include <vector>using namespace std;int main(int argc,char *argv[])
{strcat(argv[1]," ");strcat(argv[1],argv[2]);while(argc != 0){cout << argv[1] << endl;--argc;}return 0;
}
提取子串,并把字符型数字转成数值输出:
#include <iostream>
#include <string>using namespace std;int main()
{string s2 = "pi = 3.14";double d = stod(s2.substr(s2.find_first_of("+-.0123456789")));cout << d << endl;
}
使用已有算法拼接vector中的string对象:
#include <iostream>
#include <string>
#include <vector>
#include <numeric>using namespace std;int main()
{vector<string> s{"hi ","hello ","world"};string sum = accumulate(s.cbegin(),s.cend(),string(" "));cout << sum << endl;
}
一维数组,引用传参:
#include <iostream>
#include "math.h"using namespace std;
//下面这句中的9必须要写,要不然会报错,因此一维数组传入的时候,必须要知道数组的长度
void test01(const int (&r)[9])
{for(int i = 0;i < 9;++i)cout << r[i] << ' ';cout << endl;
}int main()
{int a[]={1,2,3,4,5,6,7,8,9};test01(a);system("pause");return 0;
}
c++练习代码
输入两个整数,输出这两个整数范围内的所有整数:
#include "iostream"void find(int &a,int &b)
{if(a < b){int temp;temp = a;a = b;b = temp;}
}int main()
{int v1=0,v2=0;std::cin >> v1 >> v2;find(v1,v2);for(int i = v1 - v2;i >= 0 ;--i){std::cout << v2+i << std::endl;}return 0;}
读取数量不定的输入数据:
#include "iostream"int main()
{int value=0,sum=0;while(std::cin >> value)//输入流无效的时候才会推出循环,测试的时候注意在输入数据完成后接无效输入数据作为截止 {sum += value;}std::cout << "sum的值= " << sum << std::endl;return 0;}
统计输入的数字重复的次数:
#include "iostream"int main()
{//输入的数据有几个是重复的int curren=0,cnt=0;//用来存放输入的数据 if(std::cin>>curren){int cnt = 1,val=0;//更改cntwhile(std::cin>>val){if(val==curren)++cnt;else{std::cout << curren <<" occures "<< cnt << " times " << endl;curren = val;cnt = 1;}}std::cout << curren <<" occures "<< cnt << " times " << endl;}}
指针和类型别名
#include <iostream>
using namespace std;int main()
{typedef int *p;//注意这里p是类型名,我之前就把p当成指针变量来用了 int i = 10;p p1 = &i;cout << *p1 << endl;
}
//这段代码运行会报错
#include <iostream>
using namespace std;int main()
{const int i = 12,&r = i;auto a = i;//auto会丢弃const,所以a是int类型变量,可以被赋值decltype(i) b = 10;//decltype会把i的类型给b,所以b是const int类型的常量,初始化之后不能被赋值,所以后续的赋值操作会报错a = 20;b = 1;
}
定义数据结构并赋类内初始值
#include <iostream>
#include <string>using namespace std;int main()
{struct test{int age{10};//定义数据类型的同时初始化string name{"liming"};int high{180};}; test a;cout << "age:" << a.age << " "<< "name:" << a.name << " "<< "high:" << a.high <<endl;
}
第一个结构体程序
#include <iostream>
#include <string>using namespace std;struct Salaes_date{string bookNO;double units_sold;double revenue;
};int main()
{Salaes_date data1,data2;double price = 0;cin >> data1.bookNO >> data1.units_sold >> price;data1.revenue = data1.units_sold * price;cin >> data2.bookNO >> data2.units_sold >> price;data2.revenue = data2.units_sold * price;if(data1.bookNO == data2.bookNO){unsigned totalCnt = data1.units_sold + data2.units_sold;double totalRevenue = data1.revenue + data2.revenue;cout << data1.bookNO << " " << totalCnt << " " << totalRevenue << " ";if (totalCnt != 0)cout << totalRevenue/totalCnt << endl;elsecout << "(no sales)" << endl;return 0;}else{cerr << "Data must refer to the same ISBN"<< endl;return -1;}return 0;
找出最大的字符串:
#include <iostream>
#include <string>using namespace std;string find(string s1,string s2)
{if(s1 > s2)return s1;elsereturn s2;
}int main()
{string s1 = "hello";string s2 = "hello world";//相同情况下,短的小string s3 = "hiya";//不同情况下,第一个相异字符比较来确定大小//小写字母大,字母顺序,越往后越大cout << find(s3,find(s1,s2)) << endl;//string max = find(string s3,find(s1,s2));
}
读入两个字符串,判断大小:
#include <iostream>
#include <string>using namespace std;int main()
{string s1;string s2;getline(cin,s1);getline(cin,s2);if (s1.size() == s2.size())cout << "s1 = s2" << endl;else if(s1.size() > s2.size())cout << "s1 long" << endl;elsecout << "s2 long" << endl;
}
使用范围for判断string对象中有多好个空格:
#include <iostream>
#include <string>using namespace std;int main()
{int cnt = 0;string s1("some str in g");for(auto c: s1){if (c == ' ')cnt += 1;}cout << cnt << endl;
}
计算字符串中有多少个标点符号:
#include <iostream>
#include <string>using namespace std;int main()
{string s1("hello world!!!");decltype(s1.size()) cnt = 0;for(auto c:s1){if (ispunct(c))//ispunct是cctype中的一个函数,如果是标点符号则为真cnt ++;}cout << cnt << " punctuation characters in " << s1 << endl;
}
字符串变转大写,或小写
#include <iostream>
#include <string>using namespace std;int main()
{string s1("hello world!!!");for(auto &c : s1){c = toupper(c);}cout << s1 << endl;for(auto &c : s1){c = tolower(c);}cout << s1 << endl;
}
把第一个单词转成大写:
#include <iostream>
#include <string>using namespace std;int main()
{string s1("hello world!!!");for(decltype(s1.size()) index = 0;index < s1.size() && !isspace(s1[index]);index++)//到空格结束循环{s1[index] = toupper(s1[index]);}cout << s1 << endl;
}
把所有单词首字母变成大写:
#include <iostream>
#include <string>using namespace std;int main()
{string s1("hello world!!! is me");s1[0] = toupper(s1[0]);for(decltype(s1.size()) index = 0;index < s1.size();index++){if (isspace(s1[index]))//定位空格,把空格后面的字符变成大写{s1[index + 1] = toupper(s1[index + 1]); }}cout << s1 << endl;
}
十进制转16进制:
#include <iostream>
#include <string>using namespace std;int main()
{const string hexdigits = "0123456789ABCDEF";//利用下标,下标1对应的值也正好是1,因此只要把输入的对应的值取出来就是了string result;string::size_type n;while(cin >> n){if (n < hexdigits.size()){result += hexdigits[n];cout << hexdigits[n] << endl;}else{cerr << "enter number error.please reenter" << endl;}}cout << result << endl;
}
把输入的变成大写在存入vector中:
#include <iostream>
#include <string>
#include <vector>//多余
using std::vector;using namespace std;int main()
{string word;vector<string> a;while(cin >> word){//把输入的string对象变成大写for(auto &j : word)j = toupper(j);a.push_back(word);//变成大写的string对象存入vector中}for(auto i : a)//输入vector中的内容cout << i << endl;return 0;
}
读入一组数据存入vector中,将没对相邻的数据和输出:
#include <iostream>
#include <string>
#include <vector>using std::vector;using namespace std;int main()
{int a = 0,temp = 0;vector<int> ivec;while(cin >> a){ivec.push_back(a);}for(decltype(ivec.size()) n = 0;n < ivec.size()-1;n++){cout << ivec[n] + ivec[n + 1] << ' ';}cout << endl;
}
输出头尾元素的和:
#include <iostream>
#include <string>
#include <vector>using std::vector;using namespace std;int main()
{int a = 0,temp = 0;vector<int> ivec;while(cin >> a){ivec.push_back(a);}//右边的下标,不能比左边的下标小for(decltype(ivec.size()) n = 0;n < ivec.size()-n;n++){int tal = ivec.size() - 1;//如果下标相等,则不想加if ((tal - n)==n){cout << ivec[n];break;} cout << ivec[n] + ivec[tal - n] << ' ';}cout << endl;
}
二分搜索,搜索有序数列:
#include <iostream>
#include <string>
#include <vector>using std::vector;using namespace std;int main()
{int sought = 0;cin >> sought;if (sought > 9){cerr << "please enter the right number" << endl;return -1;}vector<int> test{1,2,3,4,5,6,7,8,9};auto beg = test.begin(),end = test.end();auto mid = test.begin() + (end - beg)/2;while(mid != end && *mid != sought){if (sought < *mid)end = mid;elsebeg = mid + 1;mid = beg + (end - beg)/2;}cout << *mid << endl;
}
数组指针:
#include <iostream>
#include <string>
#include <vector>using namespace std;int main()
{//初始化数组int ia[10]={};//ia2是一个int *型变量auto ia2(ia);//ib是一个和ia一样长度的数组decltype(ia) ib;ia2 = &ia[1];*ia2 = 3;for(auto c : ib)cout << c << ' ';cout << endl;
判断数组是否相等:
#include <iostream>
#include <string>
#include <vector>
using namespace std;int main()
{constexpr size_t sz = 5;int a[sz]={1,2,3,4,5},b[6]={1,2,3,4,5};int la = end(a) - begin(a),lb = end(b) - begin(b);if (la != lb){cout << "a!=b" << endl;return -1;}else{for(int i = 0;i < la;i++){if (a[i]!=b[i]){cout << "a!=b" << endl;return -1;}}cout << "a=b" << endl;}
}
vector实现上诉问题:
#include <iostream>
#include <string>
#include <vector>using namespace std;int main()
{vector<int> a{1,2,3,4,5},b{1,2,3,4,5};if (a.size() != b.size()){cout << "a!=b" << endl;return -1;}else{for(int i = 0;i < a.size();i++){if (a[i]!=b[i]){cout << "a!=b" << endl;return -1;}}cout << "a=b" << endl;}
}
c++与c风格的字符串比较:
#include <iostream>
#include <string>
#include <vector>using namespace std;int main()
{//c++的string类型的字符串可以直接比较string s = "hello world";string s1 = "c++ dmeo";//c风格字符串,比较的时候,是比较指针而非字符串char a[] = "aello";char b[] = "bbuntu";//想要比较两个c风格的字符串需要调用strcmp函数if (strcmp(a,b)>0)cout << "a>b" << endl;elsecout << "b>a" << endl;
}
c++和c风格字符串连接:
#include <iostream>
#include <string>
#include <vector>using namespace std;int main()
{//c++的string类型的字符串可以直接比较string s = "hello world";string s1 = "c++ dmeo";//下面这段代码放在最后面就运行不了,无法输出,不知道为什么cout << s + " " + s1 << endl;//c风格字符串,比较的时候,是比较指针而非字符串char a[] = "hello";char b[] = "ubuntu";//想要比较两个c风格的字符串需要调用strcmp函数strcat(a," ");strcat(a,b);cout << a << endl;//c风格中,拷贝字符串时空间比较难以估算,这会导致危险
}
把c字符串赋值给string,通过string对象返回c类型字符串指针:
#include <iostream>
#include <string>
#include <vector>using namespace std;int main()
{string s = "hello world";char sa[] = "demo ubuntu";s = sa;cout << s << endl;const char *p = s.c_str();//c_str返回一个c风格字符串cout << *(p+5) << endl;
}
字符串拷贝:有一个字符串的指针,有目的字符串的名称,strcpy拷贝字符串
#include <iostream>
#include <string>
#include <vector>using namespace std;int main()
{string s = "hello world";const char *p = s.c_str();char sa[12] = {};strcpy(sa,p);cout << sa << endl;
}
数组引用传递:注意,数组长度和引用的长度要一致:
#include <iostream>
#include <string>
#include <vector>using namespace std;
//接收的引用的数组长度要和传入的数组长度一致,且不能留空,此处为5
void print(int (&r)[5])
{for(auto c : r)cout << c;cout << endl;
}int main()
{int a[] = {1,2,3,4,5};print(a);return 0;
}
对传入main函数的参数进行处理:
#include <iostream>
#include <string>
#include <vector>using namespace std;int main(int argc,char *argv[])
{strcat(argv[1]," ");strcat(argv[1],argv[2]);while(argc != 0){cout << argv[1] << endl;--argc;}return 0;
}
提取子串,并把字符型数字转成数值输出:
#include <iostream>
#include <string>using namespace std;int main()
{string s2 = "pi = 3.14";double d = stod(s2.substr(s2.find_first_of("+-.0123456789")));cout << d << endl;
}
使用已有算法拼接vector中的string对象:
#include <iostream>
#include <string>
#include <vector>
#include <numeric>using namespace std;int main()
{vector<string> s{"hi ","hello ","world"};string sum = accumulate(s.cbegin(),s.cend(),string(" "));cout << sum << endl;
}
一维数组,引用传参:
#include <iostream>
#include "math.h"using namespace std;
//下面这句中的9必须要写,要不然会报错,因此一维数组传入的时候,必须要知道数组的长度
void test01(const int (&r)[9])
{for(int i = 0;i < 9;++i)cout << r[i] << ' ';cout << endl;
}int main()
{int a[]={1,2,3,4,5,6,7,8,9};test01(a);system("pause");return 0;
}
发布评论