将输入直接输入到 C++ 中的向量中

发布于 2024-10-02 14:11:18 字数 217 浏览 0 评论 0原文

考虑以下代码片段:

...
int N,var;
vector<int> nums;
cin >> N;
while (N--)
{
   cin >> var;
   nums.push_back(var);
}
...

是否可以在不使用辅助变量(在本例中为 var)的情况下执行此操作?

Consider the following code piece:

...
int N,var;
vector<int> nums;
cin >> N;
while (N--)
{
   cin >> var;
   nums.push_back(var);
}
...

Is it possible to do this without using an auxillary variable, in this case var?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

暮倦 2024-10-09 14:11:18

假设您已经阅读了最初的 N,那么使用 istream_iterator 有一个不错的技巧:

std::vector<int> nums;
nums.reserve(N);
std::copy(std::istream_iterator<int>(std::cin), 
          std::istream_iterator<int>(),
          std::back_inserter(nums));

back_inserter 对象将自身转变为一个迭代器,将元素添加到最后的向量。迭代器流可以通过读取元素的类型进行参数化,如果没有给出参数,则表示输入结束。

Assuming you have already read the initial N, there is a nice trick using istream_iterator:

std::vector<int> nums;
nums.reserve(N);
std::copy(std::istream_iterator<int>(std::cin), 
          std::istream_iterator<int>(),
          std::back_inserter(nums));

The back_inserter object turns itself into an iterator that adds elements to the vector at the end. Iterator streams can be parameterized by the type of the elements read, and, if no parameter given, signals the end of input.

无声情话 2024-10-09 14:11:18

如果您的工具箱中还没有 copy_n(),那么您应该这样做。非常有用。

template<class In, class Size, class Out>
Out copy_n(In first, In last, Size n, Out result)
{
    while( n-- > 0 && first != last )
        *result++ = *first++;
    return result;
}

使用这个实用程序,可以方便而优雅地将 n 个元素复制到向量中:

#include<iterator>
#include<vector>
#include<iostream>

// ...
int n = 0;
std::cin >> n;
std::vector<int> v(n);
copy_n(std::istream_iterator<int>(std::cin), std::istream_iterator<int>(),
       n,
       v.begin());

If you don't have already copy_n() in your toolbelt then you should. Very useful.

template<class In, class Size, class Out>
Out copy_n(In first, In last, Size n, Out result)
{
    while( n-- > 0 && first != last )
        *result++ = *first++;
    return result;
}

With this utility it's convenient and elegant to copy n elements into a vector:

#include<iterator>
#include<vector>
#include<iostream>

// ...
int n = 0;
std::cin >> n;
std::vector<int> v(n);
copy_n(std::istream_iterator<int>(std::cin), std::istream_iterator<int>(),
       n,
       v.begin());
就此别过 2024-10-09 14:11:18
vector<int> nums(N);
for (int i = 0; i < N; i++)
{
    cin >> nums[i];
}

在一般情况下,这实际上效率更高。在没有初始 reserve 的情况下重复调用 std::vector::push_back() 将导致大量重新分配。

vector<int> nums(N);
for (int i = 0; i < N; i++)
{
    cin >> nums[i];
}

In the general case, this is actually more efficient. Calling std::vector::push_back() repeatedly without an initial reserve will lead to lots of reallocations.

暖树树初阳… 2024-10-09 14:11:18

迭戈·塞维利亚的回答暗示了这一点。
使用范围构造函数

std::vector<int> nums( std::istream_iterator<int>(std::cin),
                       std::istream_iterator<int>() );

Hinted from Diego Sevilla answer.
Use a range constructor

std::vector<int> nums( std::istream_iterator<int>(std::cin),
                       std::istream_iterator<int>() );
十年不长 2024-10-09 14:11:18

无需分配向量然后调整其大小。

迭代器比索引使用更可取。

size_t N;
std::cin >> N;

std::vector<int> values(N);
for (vector<int>::iterator iter = values.begin(); iter != values.end(); ++iter)
{
  std::cin >> *iter;
}

No need to allocate the vector and then resize it.

Iterators are preferable to index usage.

size_t N;
std::cin >> N;

std::vector<int> values(N);
for (vector<int>::iterator iter = values.begin(); iter != values.end(); ++iter)
{
  std::cin >> *iter;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文