第9章

顺序容器概述

顺序容器

练习 9.1:对于下面的程序任务,vector、deque和list哪种容器最为合适?解释你的选择理由。如果没有哪一种容器优于其他容器,也请解释理由。
a) 读取固定数量的单词,将它们按字典序插入到容器中。我们将在下一章中看到,关联容器更适合这个问题。

  • list或者forward_list 在中间插入效率高

b)读取未知数量的单词,总是将新单词插入到末尾。删除操作在头部进行。

  • deque

c)从一个文件读取未知数量的整数。将这些数排序,然后将它们打印到标准输出。

  • vector

容器操作:

  • 有些操作是所有容器类型都提供的
  • 有些仅针对顺序容器、关联容器、无序容器
  • 还有一些操作只适用一小部分容器

练习9.3:构成迭代器范围的迭代器有何限制?

  • 他们指向同一个容器中的元素,或者是容器最后一个元素之后的位置,且我们可以通过反复递增begin来到达end。

练习9.4:编写函数,接受一对指向vector的迭代器和一个int值。在两个迭代器指定的范围中查找给定的值,返回一个bool值指出是否找到

#include 
#include 

using namespace std;

bool findNum(vector::const_iterator beg, vector::const_iterator end, int num);
int main()
{
    vector nums = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    if(findNum(nums.begin(), nums.end(), 5))
        cout << 1;
    else 
        cout << 0;


    return 0;
}

bool findNum(vector::const_iterator beg, vector::const_iterator end, int num)
{
    for (;beg != end ; ++beg)
    {
        if (*beg == num)
        {
            return true;
        }
    }
    return false;
}

练习9.9:begin和cbegin两个函数有什么不同?
+当不需要写访问是,使用cbegin

  • begin返回的是 iterator
  • cbegin返回的是const_iterator

练习9.10:下面4个对象分别是声明类型。

    vector v1;
    const vector v2;
    auto it1 = v1.begin(), it2 = v2.begin();   //
    auto it3 = v1.cbegin(), it4 = v2.cbegin();

it1和it2应该错了,我编译器也没有运行出来。
应该是v2要返回const_iterator和v1冲突。auto的类型要保持一致所有报错
begin应该是加入没有const就返回普通iterator加入有就返回const_iterator

练习9.12 : 对于接受一个容器创建其拷贝的构造函数,和接受两个迭代器创建拷贝的构造函数,解释他们的不同。

  • 接受容器:需要容器的类型与元素类型相同
  • 接受迭代器:不需要容器类型匹配,元素只要能够转换也可以不匹配

练习9.17:假定c1和c2是两个容器,下面的比较操作有何限制,
if (c1 < c2)

  • 容器比较必须要相同容器,与相同类型的元素
  • 还要支持关系运算符


    image.png

练习9.18:从标准输入中读取string序列,存入一个deque中。编写一个循环,用迭代器打印deque中的元素

#include 
#include 
#include 
#include 

using namespace std;


int main()
{
    string str;
    deque deq;

    while (cin >> str)
    {
        deq.push_back(str);
    }
    
    for (auto i = deq.cbegin(); i != deq.cend(); ++i)
        cout << *i << " ";
    
    return 0;
}


练习9.19:编写程序,从一个list拷贝元素到两个deque中。值为偶数的所有元素都拷贝到一个deque中,而奇数值到另一个

#include 
#include 
#include 
#include 

using namespace std;


int main()
{
    list nums = {1,2,3,4,5,6,7,8,9};
    deque two_nums, one_nums;

    for (auto i = nums.cbegin(); i != nums.cend(); ++i)
    {
        if (*i % 2 == 0)
        {
            two_nums.push_back(*i);
        }
        else
            one_nums.push_back(*i);
        
    }
    
    for(auto i : one_nums)
        cout << i << " ";
    cout << endl;

    for(auto i : two_nums)
        cout << i << " ";
    cout << endl;

    return 0;
}

练习 9.25:使用下面的代码定义ia,将他copy到vector和list中然后删除vector偶数中的,删除list奇数中的

#include 
#include 
#include 

using namespace std;

int main()
{
    int ia[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89};
    vector numsv(begin(ia), end(ia));
    list numsl(begin(ia), end(ia));

    for (auto i = numsv.begin(); i != numsv.end(); )
    {
        if (*i % 2 == 0)
        {
            i = numsv.erase(i);
        }
        else
            ++i;
    }

    for (auto i : numsv)
        cout << i << " ";
    cout << endl;

    for (auto i = numsl.begin(); i != numsl.end(); )
    {
        if (*i % 2 == 0)
        {
            ++i;
        }
        else
            i = numsl.erase(i);
    }
    for (auto i : numsl)
        cout << i << " ";
    cout << endl;

    return 0;
}

练习9.27:编写函数,查找并删除forward_list中的奇数元素。

#include 
#include 

using namespace std;

int main()
{
    forward_list nums = {1,1,2,3,4,5,6,7,8,9};

    auto prev = nums.before_begin();
    auto curr = nums.begin();
    while (curr != nums.end())
    {
        if (*curr % 2)
            curr = nums.erase_after(prev);
        else 
        {
            prev = curr;
            ++curr;
        }
    }
    for (auto i : nums)
        cout << i << " ";

    return 0;
}

练习9.28:编写函数,接受一个forward_list和两个string共三个参数。函数应该在链表中查找第一个string,并将第二个string插入到紧接着第一个string之后的位置。若没有则在最后插一个

void ins(forward_list & strli, const string & fstr, const string & istr)
{
    auto prev = strli.before_begin();
    auto curr = strli.begin();

    while (curr != strli.end())
    {
        if (*curr == fstr)
        {
            curr = strli.insert_after(curr, istr);
            return;
        }
        else
        {
            prev = curr;
            ++curr;
        }
    }
    strli.insert_after(prev, istr); //curr已经指向了空不能用了
}

练习9.31:使用list或forward_list删除元素

#include 
#include 
#include 

using namespace std;

void ins(forward_list &);
void numsdl(list &);

int main()
{
    int  vi[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    forward_list v1(begin(vi), end(vi));
    list v2(begin(vi), end(vi));
    ins(v1);
    for (auto i : v1)
        cout << i << " ";
    cout << endl;
    numsdl(v2);
    for (auto i : v2)
        cout << i << " ";
    cout << endl;

    return 0;
}

void ins(forward_list & strli)
{
    auto prev = strli.before_begin();
    auto curr = strli.begin();

    while (curr != strli.end())
    {
        if (*curr % 2 == 0)
        {
            curr = strli.erase_after(prev);
        }
        else
        {
            curr = strli.insert_after(curr, *curr);
            prev = curr;
            ++curr;
        }
    }
}

void numsdl(list & strli)
{
    auto curr = strli.begin();

    while (curr != strli.end())
    {
        if(*curr % 2 == 0)
        {
            curr = strli.erase(curr);
        }
        else
        {
            
            curr = strli.insert(curr, *curr);
            ++curr;
            ++curr;
        } 
    }
}

vector扩容

vector扩容.png

练习9.35:解释一个vector的capacity和size有何区别、
capacity是最多能放多少
size是当前有多少元素

string的方法

image.png

练习 9.41:编写程序,从一个vector初始化一个string

#include 
#include 

using namespace std;

int main()
{
    vector ch = {'a', 'c', 'a'};
    string a(ch.cbegin(), ch.cend());
    cout << a;
    return 0;
}

练习9.45:编写一个函数,接受一个表示名字string参数和两个分别表示前缀和后缀的字符串。使用迭代器操作

#include 

using namespace std;

string& nameInsert(string &, const string &, const string &);

int main()
{
    string name = "jihhh";
    string first = "Mr.";
    string finally = "III";
    cout << nameInsert(name, first, finally);

    return 0;
}

string& nameInsert(string & name, const string & first, const string & finally)
{
    name.insert(name.begin(), first.begin(), first.end());
    name.append(finally.begin(), finally.end());

    return name;
}

练习9.46:重写上一题的函数,这次使用位置和长度来管理string,并且只使用insert

#include 
#include 

using namespace std;

string& nameInsert(string &, const string &, const string &);

int main()
{
    string name = "jihhh";
    string first = "Mr.";
    string finally = "III";
    cout << nameInsert(name, first, finally);

    return 0;
}

string& nameInsert(string & name, const string & first, const string & finally)
{
    name.insert(0, first);
    name.insert(name.size(), finally);

    return name;
}

练习9.47:编写程序,首先查找string "ab2c3d7R4E6"的数字,再查字母

#include 
#include 

using namespace std;

int main()
{
    string a = "ab2c3d7R4E6";
    string numbers = "0123456789";
    string::size_type pos = 0;
    while ((pos = a.find_first_of(numbers, pos)) != string::npos)
    {
        cout << a[pos];
        ++pos;
    }
    cout << endl;
    
    pos = 0;
    while ((pos = a.find_first_not_of(numbers, pos)) != string::npos)
    {
        cout << a[pos];
        ++pos;
    }
    
    return 0;
}

你可能感兴趣的:(第9章)