C++Primer第五版课后练习代码【第九章】

只是粘贴部分题目的代码,其实本章其它练习的代码也已经被包含其中了。

粘贴了一个.h文件,可以直接拷贝下来用。

#ifndef FUNCTION_9_H_INCLUDED
#define FUNCTION_9_H_INCLUDED
#include<iostream>
#include<string>
#include<list>
#include<vector>
#include<stack>
#include<forward_list>
#include<algorithm>
#include<iterator>
using namespace std;

/*9_1:
(a)由于是按字典顺序插入,所以选择链表;
(b)选择双端队列
(c)vector
*/

/*9_2:
list<deque<int>> a;
*/

/*9_3:
左闭合区间,即end不在begin之前。
*/

/*9_11:
vector<int> text;
vector<int> text(3);
vector<int> text(3,1);
vector<int> text={1,1,1};
vector<int> text(text2);
vector<int> text=text2;
vector<int> text(other_vec.begin(),other_vec.end());
*/

/*9_12:
接收一个容器创建:两个容器的类型必须相同,且容器内元素的类型也必须相同;
接受两个迭代器:两个容器的类型可以不痛,甚至容器内元素的类型也不必相同
原因:迭代器指向容器内元素的地址,可直接对元素进行操作
*/

/*9_13:
采用迭代器:
vector<int> a(10,1);
vector<double> b(a.begin(),a,end());
*/

/*9_14:
本题是两种不同类型的之间的容器进行赋值,顺序容器(除array外)定义了一个名为assign的
成员,允许我们从一个不同但相容的类型赋值,或者从容器的一个子序列赋值。
*/

void function_9_27(forward_list<int>& a)
{
    if(a.empty())
        return;
    auto prev = a.before_begin();
    auto curr = a.begin();
    while(curr!=a.end())
    {
        if(*curr%2)
        {
            curr=a.erase_after(prev);
        }
        else
        {
            prev=curr;
            ++curr;
        }
    }
}

void function_9_28(forward_list<string>& input,const string& s1,const string& s2)
{
    //auto prev=input.before_begin();
    auto curr=input.begin();

    while(curr!=input.end()&&(*curr!=s1))
    {
        ++curr;
    }
    if(curr==input.end())
    {
        cout<<"the input hasn't the s1."<<endl;
        system("pause");
        return;
    }
    else
    {
        input.insert_after(curr,s2);
    }

    auto f=find(input.begin(),input.end(),s1);
    cout<<*f<<endl;

}

void function_9_31(forward_list<int>& input)
{
    auto prev=input.before_begin();
    auto curr=input.begin();
    while(curr!=input.end())
    {
        if(*curr%2)
        {
            curr=input.insert_after(curr,*curr);
            advance(curr,1);
            advance(prev,2);
        }
        else
        {
            curr++;
            input.erase_after(prev);
        }
    }

}

/*9_32
所以不合法
++运算符高于*解引用运算符
假设容器vi={1,2,3,4,5,6}
iter=vi.begin();
iter = vi.insert(inter , *iter++);
分析:
1:第二个参数解引用是在iter递增之前,所以第二个参数是1
2:第一个参数指向2
所以是在2之前插入一个1,vi变成{1,1,2,3,4,5,6},返回的iter指向第二个1
所以会无限在2之前插入1.
*/

/*9_33
insert()函数是插入元素到输入的iterator之前
单向链表的insert_before()函数是插入元素到输入的iterator之后;
本节最后一个例子是每隔一个元素插入一个元素,若不赋予begin,则每隔两个元素插入一个元素。
*/

/*9_35
vector的capacity与size的区别:
capacity描述当前容量的大小,size返回当前元素数目.
*/

/*9_36
不可能.capacity必须大于等于size.
*/

/*9_37
list的存储空间不是连续的
array的存储空间是固定的
*/

void function_9_38(void)
{
    vector<int> temp;
    for(vector<int>::size_type i=0;i<100;i++)
    {
        temp.push_back(i);
        cout<<"capacity:"<<temp.capacity()<<"  size:"<<temp.size()<<endl;
    }
}

void function_9_41(void)
{
    vector<char> a;
    char temp;
    while(cin>>temp)
    {
        if(temp=='q')
            break;
        else
        {
            a.push_back(temp);
        }
    }
    while(cin.get()!='\n');
    string s1(a.begin(),a.end());
    cout<<"s1:"<<s1<<endl;
    string s2(a.begin()+1,a.end());
    cout<<"s2:"<<s2<<endl;
}

void function_9_43(void)
{
    string s="text the function thr ,string is so troublesome!";
    string oldVal="thr";
    string newVal="through";
    string oldVal2="so";

    auto _size = oldVal.size();
    auto _iter = s.begin();

    for(string::size_type i=0;i<(s.size()-_size+1);++i,++_iter)
    {
        string s2=s.substr(i,_size);
        if(s2==oldVal)
        {
            s.replace(i,_size,newVal);
        }
    }
    cout<<s<<endl;
}

void function_9_45(void)
{
    cout<<"Please input name:";
    string name;
    getline(cin,name);
    while(cin.get()!='\n');
    string fro = "Mr.";
    string bac = "Jr.";
    name.append(bac);
    name.insert(name.begin(),fro.begin(),fro.end());
    cout<<name<<"   "<<name.size()<<endl;
}

void function_9_46(void)
{
    cout<<"Please input name:";
    string name;
    getline(cin,name);
    while(cin.get()!='\n');
    string fro = "Mr.";
    string bac = "Jr.";
    name.insert(name.size(),bac);
    name.insert(0,fro);
    cout<<name<<"   "<<name.size()<<endl;
}

void function_9_47(void)
{
    string s1= "ad2c3d7R4E6";
    string s2="0123456789";
    string::size_type pos = 0;
    while((pos = s1.find_first_of(s2,pos))!=string::npos)
    {
        cout<<s1[pos]<<"  ";
        ++pos;
    }
    cout<<endl;
    pos=0;
     while((pos = s1.find_first_not_of(s2,pos))!=string::npos)
    {
        cout<<s1[pos]<<"  ";
        ++pos;
    }
}

/*9_48
返回npos,find()函数是寻找字符串,find_first_of()等是寻找字符。
*/

void function_9_49(void)
{
    string s1= "weruoaszxcvnm";
    string s2 = "ew sfsdgse sfss kklfgj nmvcresau kjsdghs";

    string::size_type p=0,pos = 0;

    while((pos = s2.find_first_of(s1,pos))!=string::npos)
    {
        string::size_type pos2 = pos;
        while((pos2 = s2.find_first_not_of(s1,pos)!=string::npos))
        {
            if(s2[pos2]==' ')
            {
                if(s2[pos-1]==' '||pos==0)
                {
                    string s3= s2.substr(pos,pos2-pos);
                cout << s3 << endl;
                }

            }
            pos=pos2+1;
            pos2++;
        }
        cout<<"pos:"<<pos<<"   pos2:"<<pos2<<endl;
        system("pause");
    }
}

void function_9_50(void)
{
    vector<string> a;
    float c;
    for(int i =0;i<5;i++)
    {
        string b;
        if(cin>>b)
        {
            cout <<"  ok"<<endl;
            a.push_back(b);
            c =c + stof(b);
        }
    }
    cout<<c<<endl;
}

/*function_9_51
*/
    class myTime
    {
    private:
        unsigned int year;
        unsigned int month;
        unsigned int day;
    public:
        myTime(const string & abc);
        void show(void)const
        {cout<<"year:"<<year<<endl;
        cout<<"month:"<<month<<endl;
        cout<<"day:"<<day<<endl;}
    };

    myTime::myTime(const string& abc)
    {
        string s = abc;
        string number="0123456789";
        string::size_type i= s.find_last_not_of(number);
        string y = s.substr(i+1);
        year = stoi(y);

        string js=s.substr(0,i);
        string::size_type j = js.find_last_not_of(number);
        string d = js.substr(j+1);
        day = stoi(d);

        string ks=s.substr(0,j);
        string::size_type k = ks.find_last_not_of(number);
        if(k!=string::npos)
        {
            string s3 = ks.substr(0,3);
            if (s3 == "Jan") month = 1;
            if (s3 == "Feb") month = 2;
            if (s3 == "Mar") month = 3;
            if (s3 == "Apr") month = 4;
            if (s3 == "May") month = 5;
            if (s3 == "Jun") month = 6;
            if (s3 == "Jul") month = 7;
            if (s3 == "Aug") month = 8;
            if (s3 == "Sep") month = 9;
            if (s3 == "Oct") month = 10;
            if (s3 == "Nov") month = 11;
            if (s3 == "Dec") month = 12;
        }
        else
        {
            month = stoi(ks);
        }

    }

void function_9_52(void)
{
    stack<char> word;
    string s="ACt(963597034)";
    bool flag=false;
    for(string::size_type i=0;i<s.size();i++)
    {
        if((s[i]=='(')&&(!flag))
        {
            for(i;s[i]!=')';++i)
            {
                word.push(s[i]);
            }
            word.push(' ');
        }
    }
    for(size_t i=0;i<word.size();i++)
    {

    }
}







#endif // FUNCTION_9_H_INCLUDED

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值