首页 > 编程语言 >C++primer练习14.26

C++primer练习14.26

时间:2022-08-17 21:14:03浏览次数:67  
标签:return int 练习 StrBlobPtr C++ operator curr 14.26 primer

练习14.26

为你的String类定义下标运算符

char& operator[](size_t d)
        {
            return elements[d];
        }
const char& operator[](size_t d)const 
        {
            return elements[d];
        }    

练习14.27

为你的StrBlobPtr添加递增递减运算符

    StrBlobPtr& operator++()
    {
        check(curr,"beyond");
        ++curr;
        return *this;
     } 
    StrBlobPtr& operator--()
    {
        --curr;
        check(curr,"too small");
        return *this;
     }  
    StrBlobPtr operator++(int)
    {
        StrBlobPtr ret=*this;
        check(curr,"beyond");
        ++curr;
        return ret;
     } 
     StrBlobPtr operator--(int)
    {
        StrBlobPtr ret=*this;
        --curr;
        check(curr,"too small");
        return ret;
     } 

练习14.28

为你的StrBlobPtr添加加法和减法运算符

StrBlobPtr operator+(StrBlobPtr& a,StrBlobPtr& b)
{
    StrBlobPtr c=a;
    c.curr=a.curr+b.curr;
    c.check(c.curr,"beyond");
    return c;
}
StrBlobPtr operator-(StrBlobPtr& a,StrBlobPtr& b)
{
    StrBlobPtr c=a;
    c.curr=a.curr-b.curr;
    c.check(c.curr,"too small");
    return c;
}

练习14.29

为什么不定义const版本的的递增和递减运算符

::StrBlobPtr的递增递减要修改curr

练习14.30

为你的StrBlobPtr添加解引用运算符和箭头运算符

std::string& operator*()const
    {
        auto p=check(curr,"dereference past end");
        return (*p)[curr];
    }
    std::string* operator->()const
    {
        return & this->operator*();
    }

练习14.31

我们的StrBlobPtr没有定义拷贝构造函数,赋值运算符及析构函数为什么

::因为数据是weak_ptr,无需管理内存

练习14.32

定义一个类令其含有StrBlobPtr对象的指针,为这个类重载箭头运算符

class PTR{
public:
StrBlobPtr& operator*()const
{
return (*this).ptr;
}

StrBlobPtr* operator->()const
{
return & this->operator*();
}

private:
StrBlobPtr* ptr;
}

练习14.33

一个重载的函数调用运算符应该接受几个运算对象

::N+1

练习14.34

定义一个函数对象类,令其执行if-then-else的操作:该类的调用运算符接受三个形参,首先检查第一个,成功的话返回第二个,不成功返回第三个的值

struct ite{
int operator()(int a,int b,int c)
{
if(a==0)
return b;
return c;
}
};

练习14.35

编写一个类似PrintString的类,令其从输入流读取输入,返回所读取的string,如果失败,返回空string

using namespace std;
struct ins{
    ins(istream&i):in(i){    }
    string operator()()
    {
        string a;
        in>>a;
        return a;
    }
    istream& in;
};

练习14.36

使用前一个类读取输入,并用vector保存

int main()
{
    vector<string>ha;
    ins yyy(cin);
    for(int i=1;i!=10;++i)
    a.push_back(yyy());
     for(auto d:a)
    cout<<d<<endl;
    
    
}

练习14.37

编写一个类令其检查两个值是否相等,编写程序,令其替换序列等于某个给定值的所有实例

struct equa{
    equa(const int &a):val(a){    }
    bool operator()(int &b)
    {
        return val==b;
    }
    int val;
};
#include <vector>
int main()
{

    vector<int> vi{ 0,1,2,3,4,3,4,5 };
    equa ci(2);
    replace_if(vi.begin(), vi.end(), ci,10);
   

}

练习14.38

编写一个类令其检查某个给定的string对象的长度是否与一个阈值相等,统计并报告在输入的文件中长度为1的单词有多少个、长度为2的单词有多少个

struct thanl{
    thanl(const size_t a):val(a){    }
    bool operator()(string &b)
    {
        return val==b.size();
    }
    size_t val;
};
#include <vector>
#include <fstream>
int main()
{
    ifstream fin("number.txt");
    vector<int> vi{ 0,1,2,3,4,3,4,5 };
    thanl ci(2);
    string word;
    int count =0;
    while(fin>>word)
    {
        if(ci(word))
        ++count;
    }
   
cout<<count<<endl;
}

练习14.39

修改上一题程序令其报告长度在1至9之间的有多少,10以上的有多少

struct thanl{
    thanl(const size_t a):val(a){    }
    bool operator()(string &b)
    {
        return b.size()>1&&b.size()<9;
    }
    size_t val;
};
#include <vector>
#include <fstream>
int main()
{
    ifstream fin("number.txt");
    vector<int> vi{ 0,1,2,3,4,3,4,5 };
    thanl ci(2);
    string word;
    int count =0;
    while(fin>>word)
    {
        if(ci(word))
        ++count;
    }
   
cout<<count<<endl;
}
struct thanl{
    thanl(const size_t a):val(a){    }
    bool operator()(string &b)
    {
        return b.size()>10;
    }
    size_t val;
};

练习14.41

你认为C++11新标准为什么要增加lambda?

::临时使用更方便吧

练习14.42

使用标准库函数对象及适配器定义一条表达式

(a)统计大于1024的值有多少

count_if(iv.begin(), iv.end(), bind(std::greater<int>(),_1, 1024))

(b)找到第一个不等于pooh的字符串

find_if(svec.cbegin(), svec.cend(), bind(std::not_equal_to<string>(), _1, "pooh"));

(C)将所有的值乘以2

transform(iv.begin(), iv.end(), iv.begin(), bind(std::multiplies<int>(), _1, 2));

 

练习14.43

使用标准库对象判断一个给定的int值是否能被int容器中的所有元素整除

any_of(iv.begin(), iv.end(), bind(std::modulus<int>(), 1024, _1));

 

标签:return,int,练习,StrBlobPtr,C++,operator,curr,14.26,primer
From: https://www.cnblogs.com/yddl/p/16596747.html

相关文章

  • C/C++ 如何拷贝一个wchar_t类型的字符串
    1Dothis,23wchar_tclone[260];45wcscpy(clone,szPath);67Or,ifyouwanttoallocatememoryyourself,89wchar_t*clone=newwchar_t[wc......
  • C++ 起别名
    可以基于typedef、using等关键词实现typedef std::vector<int>intvec;using intvec =std::vector<int>; //这两个写法是等价的另一个例子,函数指针:typedefvoid(*......
  • 基于C++的OpenGL 13 之Mesh
    1.引言本文基于C++语言,描述OpenGL的Mesh前置知识可参考:基于C++的OpenGL12之多光源-当时明月在曾照彩云归-博客园(cnblogs.com)笔者这里不过多描述每个名词、......
  • 基于C++的OpenGL 14 之模型加载
    1.引言本文基于C++语言,描述OpenGL的模型加载前置知识可参考:基于C++的OpenGL13之Mesh-当时明月在曾照彩云归-博客园(cnblogs.com)笔者这里不过多描述每个名词......
  • C++primer练习14.10-23
    练习14.10对于Sales_data的输入运算符来说给定下面的输入会发生什么?(a)0-201-99999-91024.95正常输入(b)1024.950-210-99999-9最后一个输入格式错误,会chongz练习14.11......
  • C++ 类型转换
    1、std::string转intstringvalStr="IconVisible"; intvalInt=atoi(valStr.c_str()); 2、std::string转char*stringvalStr="IconVisible";constchar*......
  • C++ 获得`wchar_t *`的长度
    sizeof(wchar_t);如果你想知道一个wchar_t串(wchar_t*)的大小,要使用wcslen:1size_twcslen(constwchar_t*ws);假设你想终止的C风格的字符串,你有两个选择空的长......
  • 2.C/C++的const
    1.C语言的const修饰的变量都有空间2.C语言的const修饰的全局变量具有外部链接属性3.C++语言的const修饰的变量有时有空间,有时没有空间(发生常量折叠,且没有对变量进行取址......
  • C++primer练习14.1-9
    练习14.1在什么时候情况下重载的运算符与内置运算符有所区别?在什么时候重载的运算符又与内置运算符一样::为类设计的运算符,尽量重载的运算符含义不要改变,如+还是加法练习1......
  • C++ typeid获取类型信息
    #include<iostream>#include<typeinfo>intmain(){inti;conststd::type_info&info=typeid(int);std::cout<<"typeid"<<info.name()<<std::......