首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++初级版第5版练习3.24 -向量迭代器

C++初级版第5版练习3.24 -向量迭代器
EN

Stack Overflow用户
提问于 2014-11-05 18:39:27
回答 5查看 1.4K关注 0票数 3

2014年9月,我开始使用Lippman,Lajoie &Moo的C++ Primer第五版(第5版,2014年5月)自学C++。在那本书里我可以做一些练习,有些我不得不在这里寻求帮助,而在这一次我被困了好几天。我搜索谷歌,博客和其他论坛,但一无所获,所以我请求你的帮助。这是第113页的练习3.24,要求与第105页中的练习3.20相同,但使用迭代器:

将一组整数读入向量。打印每对相邻元素的和。更改您的程序,以便它打印第一个和最后一个元素的和,然后是第二个和第二个到最后的和,依此类推。

按照它的要求使用迭代器,我可以完成第一部分:

代码语言:javascript
复制
#include <iostream>
#include <vector>

using std::cin; using std::cout; using std::endl; using std::vector;

int main()
{
   vector<int> lista;
   int num_entra = 0;

   while (cin >> num_entra)
       lista.push_back(num_entra);


   cout << "Sum of adjacent pairs: " << endl;
   for (auto it = lista.begin(); it != lista.end() - 1; ++it)
   {
       *it += *(it + 1);
       cout << *it << ' ';
   }

   return 0;
}

上面的代码按预期工作,但我需要将第一和最后、第二和第二相加到最后.等等,我就是解决不了一个问题:

代码语言:javascript
复制
int main()
{
   vector<int> lista;
   int num_entra = 0;

   while (cin >> num_entra)
       lista.push_back(num_entra);


   auto ult = lista.end();

   cout << "Sum of the first and last elements until the center: " << endl;
   for (auto it = lista.begin(); it != lista.end() - 1; ++it)
   {
       *it = *it + *(--ult);
       cout << *it << ' ';
   }

   return 0;
}

如果用户进入1 2 3 4 5 6,程序将按需要添加1+ 6、2+5和3+4,

但是它将最后的结果(7)加上5,然后加上6,我似乎找不到阻止这种行为的方法。我能做些什么呢?程序只显示每对的和一次,直到列表的中心?提前谢谢你。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2014-11-05 18:50:09

一种方法是使用两个迭代器。

代码语言:javascript
复制
auto front = lista.begin();  // start at front and increment
auto back = lista.end() - 1; // start at back and decrement
for (;
     back > front;     // stop when the iterators cross each other
     ++front, --back)
{
    int total = *front + *back;
    std::cout << total << ' ';
}
票数 2
EN

Stack Overflow用户

发布于 2014-11-05 19:00:35

我的五分钱。只应该像以前那样输入值,而不是显式初始化的向量。

代码语言:javascript
复制
#include <iostream>
#include <vector>

int main() 
{
    std::vector<int> v = { 1, 2, 3, 4, 5 };

    if ( !v.empty() )
    {
        auto first = v.cbegin(), last = v.cend();
        do
        {
            std::cout << *first + *--last;
        } while ( first++ != last );
    }

    return 0;
}

输出是

代码语言:javascript
复制
666

例如,这种方法也可以用于std::list

如果您需要跳过中间的奇数元素,那么对于没有随机访问迭代器的容器,一般的方法如下

代码语言:javascript
复制
#include <iostream>
#include <vector>

int main() 
{
    std::vector<int> v = { 1, 2, 3, 4, 5 };

    for ( auto first = v.cbegin(), last = v.cend(); 
          first != last && first != --last;
          ++first )
    {
        std::cout << *first + *last;
    }

    std::cout << std::endl;

    return 0;
}

由于向量有随机存取迭代器,所以可以使用operator <重写代码。

代码语言:javascript
复制
#include <iostream>
#include <vector>

int main() 
{
    std::vector<int> v = { 1, 2, 3, 4, 5 };

    if ( !v.empty() )
    {
        for ( auto first = v.cbegin(), last = v.cend(); 
              first < --last;
              ++first )
        {
            std::cout << *first + *last;
        }

        std::cout << std::endl;
    }
    return 0;
}       

在这两种情况下,输出将是

代码语言:javascript
复制
66
票数 0
EN

Stack Overflow用户

发布于 2014-11-05 19:09:04

代码的问题是,当您通过取消迭代器来计算和时,您正在修改向量(可能不是一个问题,但这可能是一个不必要的副作用),而且当您“超过”向量的中间位置时,甚至不会停止循环。

简单的版本可以是

代码语言:javascript
复制
auto front = lista.begin();
auto back = lista.end() - 1;
for (; back > front; ++front, --back)
{
    int total = *front + *back;
    std::cout << total << ' ';
}

但这并不能处理像1 2 3 4 5 6 7这样的奇数元素(中央4没有打印)。如果您不想要任何“相同的元素”耦合,这是好的,这个解决方案将处理您需要的每一种情况,否则继续。

固定版本可能是

代码语言:javascript
复制
auto front = lista.begin();
auto back = lista.end() - 1;
for (; back >= front; ++front, --back)
{
    if (front == back)
        cout << *front;
    else {
        int total = *front + *back;
        std::cout << total << ' ';
    }
}

但这并不能处理空列表[]

这个解决方案可以同时处理这两种情况,尽管它更复杂:

代码语言:javascript
复制
auto it = lista.begin();
    auto ult = lista.rbegin();
    size_t dist;
    for ( ; it != lista.end() && // I have a valid front iterator
            ult != lista.rend() && // I have a valid back iterator
            (dist = std::distance(it,ult.base()), dist) > 0; // Their distance is greater than 0 (1 is included)
    ++it,++ult ) {
        if (dist == 1)
            cout << *it;
        else
            cout << *it + *ult << ' ';
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26764617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档