C++ Primer习题笔记

来源:互联网 发布:阿里云 手机归属地 编辑:程序博客网 时间:2024/06/06 11:40

1.利用指针将数组中的元素置为0:

<span style="font-size:14px;"><span style="font-size:14px;">const int a = 5;    int arr[a] = { 0, 1, 2, 3, 4};    for (int *pBegin = arr; pBegin != arr+a; pBegin++)           //数组的遍历    {        *pBegin = 1;        cout << *pBegin;    }</span></span>

2.分别比较2个数组和2个vector对象是否相等:
<span style="font-size:14px;"><span style="font-size:14px;">bool compare(int* const pb1, int* const pe1, int* const pb2, int* const pe2){  if ((pe1 - pb1) != (pe2 - pb2)) // have different size.    return false;  else  {    for (int* i = pb1, *j = pb2; (i != pe1)&&(j != pe2); ++i, ++j)      if (*i != *j) return false;  }    return true;}int main(){    int arr1[3] = {0, 1, 2};    int arr2[3] = {0, 2, 4};        if (compare(begin(arr1), end(arr1), begin(arr2), end(arr2)))      cout << "The two arrays are equal." << endl;    else      cout << "The two arrays are not equal." << endl;          cout << "==========" << endl;          vector<int> vec1 = {0, 1, 2};    vector<int> vec2 = {0, 1, 2};        if (vec1 == vec2)      cout << "The two vectors are equal." << endl;    else      cout << "The two vectors are not equal." << endl;    return 0;}</span></span>

3.输入字符串,输出次数出现最多的单词,如:how, now now now brown cow cow now出现了3次。
<span style="font-size:14px;"><span style="font-size:14px;">string preWord, currWord;       //当前输入的单词及前一次单词  string repWord;                 //重复次数最多的单词  //当前单词的重复次数及单词重复的次数最大值  int currCnt = 0, maxCnt = 1;  cout << "Enter some words(Ctr+Z to end): "<< endl;  while(cin >> currWord)  {  if(currWord == preWord)     //当前单词重复出现  ++currCnt;  else //出现新单词  {  if(currCnt > maxCnt)//出现了重复次数更多的单词,更新4  {  maxCnt = currCnt;  repWord = preWord;  }  currCnt = 1;  }  preWord = currWord;  }  if(currCnt > maxCnt)  {  maxCnt = currCnt;  repWord = preWord;  }  if(maxCnt != 1)  cout << '"' << repWord << '"' << " repeated for " << maxCnt << " times!" << endl;  else  cout << "There is no repeated word." << endl;  </span></span>


4.逆序输出vector容器中的内容:

方法一:

<span style="font-size:14px;"><span style="font-size:14px;">int count = vecInt.size();for (iter=vecInt.begin();iter!=vecInt.end();iter++){cout<<vecInt[--count]<<"   ";}</span></span>
方法二:反向迭代器
<span style="font-size:14px;"><span style="font-size:14px;">vector<int>::reverse_iterator iter;for (iter=vecInt.rbegin();iter!=vecInt.rend();iter++){cout<<*iter;}</span></span>

5.写一个函数将"tom is cat" 倒序打印出来,即 "cat is tom"。
<span style="font-size:14px;">#include<iostream>using namespace std;//反字符串#define SPACE ' '  //或者使用const char SPACE = ' ';int main(){    const char* str = "Tom is cat"; // 字符串    const char* first= str+strlen(str)-1;    const char* second = first + 1; // 开始时 俩都指向字符串结尾处    while(str != first--)       //指针从结尾处开始循环递减到 字符串为空的    {        if(SPACE == *first)     //第一个单词        {            for (int i = 1; first + i != second; ++i)            {                cout << *(first + i);            }            cout << " ";            second = first;        }        if (str == first)        {            for (int i = 0; first + i != second; ++i)            {                cout << *(first + i);            }            cout << endl;        }    }    return 0;}</span>

6.打印图形 函数

*
*.*.
*..*..*..
*...*...*...*...

<span style="font-size:14px;">#include<iostream>using namespace std;const int N = 6;void getPic(){    for (int i = 1; i <= N; i++)    {        for (int j = 1; j <= i; j++ ) //打印 * 号,每行的个数与行数相当。        {            cout << "*";            for (int k = 1; k < i; k++)//打印 . 每行的个数比行数少一个。            {                cout << ".";            }        }        cout<<endl;    }}int main(){    getPic();    return 0;}</span>




















0 0
原创粉丝点击