![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
c++primer部分习题
陳紋欽
这个作者很懒,什么都没留下…
展开
-
计算底数的指数次方
#includeusing namespace std;int main(){ int base, exponent; long result = 1; cout << "enter base and exponent:" << endl; cin >> base >> exponent; if(exponent < 0) { cout << "exponent can'原创 2013-06-16 21:54:45 · 853 阅读 · 0 评论 -
vector元素都是指向string的指针
#include#include#includeusing namespace std;//定义一个vector对象,其每个元素都是指向string类型的指针//读取该vector对象,输出每个string的内容及长度int main(){ vector spvec; //读取vector对象 string str; cout << "Enter some string原创 2013-06-17 22:47:33 · 917 阅读 · 0 评论 -
vector下标操作
int main(){ vector ivec(10,4); for(vector::size_type ix = 0; ix != ivec.size(); ix++) //用int=0没错 ivec[ix] = 2; for(vector::size_type ix = 0; ix != ivec.size(); ix++) cout << ivec[ix] << endl;原创 2013-06-17 22:35:38 · 1050 阅读 · 0 评论 -
统计读入文本中双字符出现的次数
int main() //统计读入文本中双字符ff,fl,fi出现的次数{ char currCh, pch = 'a';//分别记录当前读入字符及前一字符 //初始化各个计数器 int ffcnt = 0, flcnt = 0, ficnt = 0; while (cin >> currCh) { if(pch == 'f') //若前一个字符时'f' switch原创 2013-06-16 22:44:19 · 592 阅读 · 0 评论 -
while 记录当单词重复出现
int main(){ string currWord,preWord; //记录当前单词及前一单词 cout << "Enter some strings(Ctrl+Z to end):" << endl; while(cin >> currWord) { if(currWord == preWord) //当单词重复出现 if(currWord == preWord && is原创 2013-06-16 22:55:54 · 489 阅读 · 0 评论 -
switch-case
int main(){ char ch; //初始化计数器 int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0; int spaceCnt = 0,tabCnt = 0, newlineCnt = 0; while (cin.get(ch)) { //若ch是元音字母以及空格,制表符和换行符,将相应计数器加1 switc原创 2013-06-16 22:33:56 · 8528 阅读 · 0 评论 -
string的size求对象中个数
int main(){ string s("ni shi sb!"); cout << "The size of s is " << s.size() << endl; return 0;}原创 2013-06-16 22:25:52 · 476 阅读 · 0 评论 -
string对象相加
int main(){ string s1 = "hello"; string s2 = "world"; string s3 = s1 + ","; //string s4 = "hello" + ","; //将两个字符串面值相加是非法的 string s5 = s1 + "," + "world"; //string s6 = "hello" + "," + s2;}原创 2013-06-16 22:14:30 · 665 阅读 · 0 评论 -
do-while例子
int main(){ string str1,str2,str3; do{ cout << "Enter two strings:" << endl; cin >> str1 >> str2; if(str1 < str2) cout << "str1 < str2" << endl; else if(str1 > str2) cout str2" << en原创 2013-06-16 22:08:24 · 910 阅读 · 0 评论 -
输入输出一行文本
#include#includeusing namespace std;int main() //输入输出一行文本{ string line; while (getline(cin,line)) cout << line << endl; return 0;}原创 2013-06-16 21:59:33 · 623 阅读 · 0 评论 -
while当输入string重复时跳出
int main(){ string currWord,preWord; //记录当前单词及前一单词 cout << "Enter some strings(Ctrl+Z to end):" << endl; while(cin >> currWord) { if(currWord == preWord) //当单词重复出现 if(currWord == preWord && is原创 2013-06-20 12:01:07 · 553 阅读 · 0 评论