C++ primer 5th edition 第三章代码手写

#include <string>
#include <iostream> 
#include<cstdlib>
#include <vector>
#include <cstring>

using namespace::std;


//3.36
bool compare(int *const pb1, int *const pe1, int *const pb2, int *const pe2) {
    if ((pe1 - pb1) != (pe2 - pb2)) {
        return false;
    }
    else{
        for (int *i = pb1, *j = pb2; i != pe1, j != pe2; ++i, ++j) {
            if (*i != *j) return false;
        }
    }
}
int main()
{
    //76
    //    string word;
    //    for(;cin>>word;)
    //      if (word=="0")
    //      break;
    //      cout<<word<<endl;
    //     
    //      
    //    cout<<word;
    //    return 0;
    //78
    /*string line;
    while (getline(cin, line))
    {
        cout << line << endl;
        decltype(line.size())size = line.size();
        cout << size << endl;
        bool a = (' x' < 'X');
        cout << a;
    }*/
    //86.1
    /*string str("xrty21342 er");
    for (auto &c : str) {
        if (isalpha(c)) c = 'X';
    }
    cout << str << endl;*/
    //86.2
    /*string str("xrty21342 er");
    for (char &c : str) {
        if (isalpha(c)) c = 'X';
    }
    cout << str << endl;*/
    //86.3
    //(1)
    /*string str("xrty21342 er");
    decltype(str.size())index = 0;
    while(index< str.size()) {
        auto &c = str[index];
        if (isalpha(c)) c = 'X';
        ++index;
    }
    cout << str << endl;*/
    //(2)
    /*string str("xrty21342 er");
    decltype(str.size())index = 0;
    for (index;index<str.size();index++) {
        auto &c = str[index];
        if (isalpha(c)) c = 'X';
    }
    cout << str << endl;*/
    //86.5
    /*string str;
    string str1;
    cout << "pls enter a phrase with punctions" << endl;
    cin >> str;
    for (auto c : str) {
        if (!ispunct(c)) str1 += c;
    }
    cout << str1 << endl;
*/
    //91.1
/*vector<int>i1;
    int num;
    while (cin >> num) i1.push_back(num);
    for (auto c : i1) {
        cout << c << endl;
    }
*/
    /*vector <string> str;
    string word;
    while (true)
    {
        cin >> word;
        str.push_back(word);
        if (cin.get() == '\t')    
        {
            break;
        }
        
    }                                                          
    for (auto c : str) {
        cout << c << endl;
    }*/
    //94.2
    /*vector <string> str;
    string word;
    while (cin >> word)
    {
        str.push_back(word);
    }
    for (auto &s : str)
    {
        for (auto &c : s)
            c=toupper(c);
    }

    for (auto s : str) {
        for (auto c : s)
        {
            cout << c;
        }
        cout << endl;
    }*/
//94.4
//vector<int>i1;
//int num;
//cout << "请输入一列数,每输入一个数按下回车" << endl;
//while (cin >> num) i1.push_back(num);
//decltype(i1.size())index = 0;
//for (index; index < i1.size(); ++index) {
//    if (index < (i1.size()) / 2) {
//        int num1 = i1[index] + i1[i1.size() - 1 - index];
//        cout << "第" << index + 1 << "和倒数第" << index + 1 << "的和为:" << num1 << endl;
//    }
//}
//99.2
//vector<string> text;
//string word;
//while (cin >> word)
//{
//    text.push_back(word);
//}
//for (auto it = text.begin(); it != text.end() && !it->empty(); ++it) {
//    //for (auto &a : *it)
//    for(auto a=(*it).begin();a!=(*it).end();a++)
//    {
//        *a=toupper(*a);
//    }
//
//}
//for (auto it2 = text.begin(); it2 != text.end() && !it2->empty(); ++it2) {
//    cout << *it2 << endl;
//}
//99.3
//vector<int>num{ 1,2,3,4,5,6,7,8,9,10 };
//for (auto it = num.begin(); it != num.end(); it++) {
//    *it = (*it) * 2;
//}
//for (auto it1 = num.begin(); it1 != num.end(); it1++) {
//    cout << *it1 << endl;
//}
//101.1
//vector<int>i1;
//int num;
//cout << "请输入一列数,每输入一个数按下回车" << endl;
//while (cin >> num) i1.push_back(num);
//for (auto c=i1.begin();c<(i1.begin()+i1.size()/2);c++)
//{
//    auto b = i1.begin() + (i1.end() - c)-1;
//    int a = *c + * b;
//    cout  << a << endl;
//    }
//101.2
//vector<unsigned> scores(11, 0);
//unsigned grade;
//while (cin >> grade)
//{
//    if (grade <= 100) {
//        auto a = scores.begin();
//        a = a + (grade / 10);
//        (*a)++;
//    }
//}
//for (auto c : scores) {
//    cout << c << endl;
//}
//104.1
//int array[10];
//int index = 0;
//for (auto &c : array) {
//    
//    c = index;
//    ++index;
//}
//for (auto d : array) {
//    cout << d << endl;
//}
//int array1[10];
//for (auto &e : array1) {
//    for (auto d : array) {
//        e = d;
//    }
//}或者直接用b[i]=a[i]对i循环
//for (auto d : array) {
//    cout << d << endl;
//}
//104.2
//vector<int>vec;
//vector<int>vec1;
//for (int i = 0; i < 10; i++) {
//    vec.push_back(i);
//}
//for (auto c : vec) {
//    cout << c;
//}
//cout << endl;
//vec1 = vec;
//for (auto c : vec1) {
//    cout << c;
//}
//3.35
    /*int array[10] = { 1,2,3 };
int *beg = begin(array);
for (beg; beg!=end(array); beg++) {
    *beg = 0;
}
for (auto c : array) {
    cout << c;
}*/
//3.36
 /*int array1[10] = { 1,2,3 };
int array2[10] = { 1,2,3 };
for (int i = 0; i < 10; i++) {
    if (array1[i] = array2[i]) {
        cout << "equal";
        break;
    }
}
for (int i = 0; i < 10; i++) {
    if(array1[i] != array2[i]) {
        cout << "not equal";
        break;
    }
}*/
//写成函数更好
 /*int array1[10] = { 1,2,3,4 };
int array2[10] = { 1,2,3 };
if (compare(begin(array1), end(array1), begin(array2), end(array2))) cout << "equal"<<endl;
else cout << "not equal"<<endl;
vector<int> vec1 = { 0,1,2,3 };
vector<int> vec2 = { 0,1,2 };
if (vec1 == vec2) cout << "equal" << endl;
else cout << "not equal"<< endl;*/
//3.37
 /*const char ca[] = { 'h', 'e', 'l', 'l', 'o'};
const char s[] = "world";
const char *cp = ca;
while (*cp) {
    cout << *cp;
    ++cp;
}*///o后面没有/0,导致会继续输出ca下标范围外的值,造成错误
//3.40
 /*const char a[] = "asd";
const char b[] = "zxc";
char c[8];
strcpy(c, a);
strcat(c, " ");
strcat(c, b);
for (auto d : c) {
    cout << d;
}*/
//要加上#pragma warning(disable : 4996)头
//3.41
 /*int a[] = { 1,2,3,4,5 };
vector<int> b(begin(a), end(a));
for (auto c : b) {
    cout << c;
}*/
 /*int a[] = { 1,2,3,4,5 };
vector <int> b;
for (int *c = begin(a); c != end(a); c++) {
    b.push_back(*c);
}
for (auto d : b) {
    cout << d;
}*/
//3.42
//vector <int> b(10,0);
//auto size = b.size()+1;
//int * a = new int[size] ;
//int index = 0;
//for (auto c : b) {
//    a[index] = c;
//    index++;
//}
//for (int i = 0; i < 10; i++) cout << a[i] << " ";
//cout << endl;
//delete []a;
 /*vector<int> ivec{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int int_arr[10];

for (int* i = begin(int_arr); i != end(int_arr); ++i)
*i = ivec[i - begin(int_arr)];

for (auto i : int_arr) cout << i << " ";
cout << endl;*/
//3.43
int ia[2][3] = { 1,2,3,4,5,6 };
//version1
for(int (&row)[3]:ia)//int_array &row;
for (int col : row) {
    cout << col<<" ";
}
cout << endl;
//version2
for(size_t i=0;i<2;i++)
    for (size_t j = 0; j < 3; j++) {
        cout << ia[i][j] << " ";
    }
cout << endl;
//version3
using int_array = int[3];//typedef int int_array[3]
for (int_array *p = begin(ia); p != end(ia); p++) //int (*p)[3] =...(ord)
for (int *q = begin(*p); q != end(*p); q++) {
    cout << *q << " ";
}
    system("pause");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值