2014年9月,我开始使用Lippman,Lajoie &Moo的C++ Primer第五版(第5版,2014年5月)自学C++。在那本书里我可以做一些练习,有些我不得不在这里寻求帮助,而在这一次我被困了好几天。我搜索谷歌,博客和其他论坛,但一无所获,所以我请求你的帮助。这是第113页的练习3.24,要求与第105页中的练习3.20相同,但使用迭代器:
将一组整数读入向量。打印每对相邻元素的和。更改您的程序,以便它打印第一个和最后一个元素的和,然后是第二个和第二个到最后的和,依此类推。
按照它的要求使用迭代器,我可以完成第一部分:
#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;
}上面的代码按预期工作,但我需要将第一和最后、第二和第二相加到最后.等等,我就是解决不了一个问题:
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,我似乎找不到阻止这种行为的方法。我能做些什么呢?程序只显示每对的和一次,直到列表的中心?提前谢谢你。
发布于 2014-11-05 18:50:09
一种方法是使用两个迭代器。
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 << ' ';
}发布于 2014-11-05 19:00:35
我的五分钱。只应该像以前那样输入值,而不是显式初始化的向量。
#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;
}输出是
666例如,这种方法也可以用于std::list
如果您需要跳过中间的奇数元素,那么对于没有随机访问迭代器的容器,一般的方法如下
#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 <重写代码。
#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;
} 在这两种情况下,输出将是
66发布于 2014-11-05 19:09:04
代码的问题是,当您通过取消迭代器来计算和时,您正在修改向量(可能不是一个问题,但这可能是一个不必要的副作用),而且当您“超过”向量的中间位置时,甚至不会停止循环。
简单的版本可以是
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没有打印)。如果您不想要任何“相同的元素”耦合,这是好的,这个解决方案将处理您需要的每一种情况,否则继续。
固定版本可能是
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 << ' ';
}
}但这并不能处理空列表[]。
这个解决方案可以同时处理这两种情况,尽管它更复杂:
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 << ' ';
}https://stackoverflow.com/questions/26764617
复制相似问题