如何使用for_each输出到cout?

发布于 2024-10-02 09:04:40 字数 282 浏览 8 评论 0 原文

有没有更直接的方法来做到这一点?

for_each(v_Numbers.begin(), v_Numbers.end(), bind1st(operator<<, cout));

如果可能的话,没有显式的 for 循环。

编辑:

如果可能的话,如何使用 std::vectorstd::cin 执行此操作? (如何仅读取 n 个元素)?

Is there a more straight-forward way to do this?

for_each(v_Numbers.begin(), v_Numbers.end(), bind1st(operator<<, cout));

Without an explicit for loop, if possible.

EDIT:

How to do this for std::cin with a std::vector if possible? (How to read n elements only)?

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

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

发布评论

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

评论(6

A君 2024-10-09 09:04:40

您可以使用 std::copy 来实现此目的进入 std::ostream_iterator

std::vector<int> v_Numbers; // suppose this is the type
// put numbers in
std::copy(v_Numbers.begin(), v_Numbers.end(),
          std::ostream_iterator<int>(std::cout));

它会如果添加一些后缀就更好了:

std::copy(v_Numbers.begin(), v_Numbers.end(),
          std::ostream_iterator<int>(std::cout, "\n"));

这假设您的容器是一个 vector,因此您必须用适当的类型替换该部分。

关于读取输入的编辑

相反,您可以从 使用std::istream_iterator转换为向量 rel="nofollow noreferrer">std::back_inserter

std::vector<int> v_Numbers;
std::copy(std::istream_iterator<int>(std::cin), std::istream_iterator<int>(),
          std::back_inserter(v_Numbers));

如果只想读取n个元素,请查看这个问题

You could achieve this using std::copy into a std::ostream_iterator:

std::vector<int> v_Numbers; // suppose this is the type
// put numbers in
std::copy(v_Numbers.begin(), v_Numbers.end(),
          std::ostream_iterator<int>(std::cout));

It would be even nicer if you add some suffix:

std::copy(v_Numbers.begin(), v_Numbers.end(),
          std::ostream_iterator<int>(std::cout, "\n"));

This assumes that your container is a vector<int>, so you will have to replace that part with the appropriate type.

Edit regarding reading input:

Conversely, you can copy from a range of std::istream_iterator into a vector using std::back_inserter:

std::vector<int> v_Numbers;
std::copy(std::istream_iterator<int>(std::cin), std::istream_iterator<int>(),
          std::back_inserter(v_Numbers));

If you want to read n elements only, look at this question.

撑一把青伞 2024-10-09 09:04:40

另一种选择 - Boost.Lambda

for_each(v.begin(), v.end(), cout << boost::lambda::_1);

Another option — Boost.Lambda.

for_each(v.begin(), v.end(), cout << boost::lambda::_1);
如歌彻婉言 2024-10-09 09:04:40

是的,但你必须使用 std::copy 算法:

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

int main()
{
    std::vector<int> a;
    // fill a...
    std::copy(a.begin(), a.end(), std::ostream_iterator<int>(std::cout));
}

Yep, but you must use std::copy algorithm:

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

int main()
{
    std::vector<int> a;
    // fill a...
    std::copy(a.begin(), a.end(), std::ostream_iterator<int>(std::cout));
}
苏佲洛 2024-10-09 09:04:40

是的,使用 lambda 表达式 (C++ 11),我们可以将 STL 容器的每个元素内联打印到 cout。

#include <iostream>   // cout
#include <vector>     // vector
#include <algorithm>  // for_each
#include <iterator>   // istream_iterator
using namespace std;

int main()
{
   std::vector<int> v(10,2);
   std::for_each(v.begin(), v.end(), [](int i)->void {std::cout << i <<endl;});
   return 0;
}

用于将“n”个值从 cin 读取到向量,

 int main()
 {
   std::vector<int> v;

   int elementsToRead;
   cin>>elementsToRead;  // Number of elements to copy

   // Reading from istream
   std::istream_iterator<int> ii2(std::cin);
   std::copy_n(ii2, elementsToRead, std::back_inserter(v));

   // printing updated vector
   std::for_each(v.begin(), v.end(), [](int i)->void {cout << i <<endl;});

   return 0;
}

(或)使用 Lambda 表达式

std::for_each(std::istream_iterator<int>(cin),std::istream_iterator<int>(),[&v](int i)->void { v.push_back(i);});

要了解有关 Lambda 表达式的更多信息 @ C++11 中的 lambda 表达式是什么?

yup, using lambda expression (C++ 11) we can inline printing of each element of a STL container to cout.

#include <iostream>   // cout
#include <vector>     // vector
#include <algorithm>  // for_each
#include <iterator>   // istream_iterator
using namespace std;

int main()
{
   std::vector<int> v(10,2);
   std::for_each(v.begin(), v.end(), [](int i)->void {std::cout << i <<endl;});
   return 0;
}

For reading "n" values from cin to vector,

 int main()
 {
   std::vector<int> v;

   int elementsToRead;
   cin>>elementsToRead;  // Number of elements to copy

   // Reading from istream
   std::istream_iterator<int> ii2(std::cin);
   std::copy_n(ii2, elementsToRead, std::back_inserter(v));

   // printing updated vector
   std::for_each(v.begin(), v.end(), [](int i)->void {cout << i <<endl;});

   return 0;
}

(or) by using Lambda expression

std::for_each(std::istream_iterator<int>(cin),std::istream_iterator<int>(),[&v](int i)->void { v.push_back(i);});

To know more about Lambda expression @ What is a lambda expression in C++11?

佞臣 2024-10-09 09:04:40

在公司代码中并不总是合适,但为了枚举选项 - 如果您确实发现其他 for_each / std::copy 等解决方案太冗长,您可以写:

std::ostream& operator(std::ostream& os, const std::vector<My_Type>& v)
{
     // pick one of the other implementations for here...
    std::copy(std::istream_iterator<My_Type>(os), std::istream_iterator<My_Type>(),   
          std::back_inserter(v_Numbers));   
}

如果您彬彬有礼,那就更好了 (;-p )足以仅重载您的特定向量实例(这要求 My_Type 不仅仅是一个 typedef 来表示 int,尽管创建一个模板类来创建包装任意类型的新类型并不难)。否则,如果其他人在您的翻译单元的其他地方执行相同的操作,则流式传输可能会变得不明确。

Not always appropriate in corporate code, but for the sake of enumerating options - if you really find other for_each / std::copy etc. solutions too verbose, you could write:

std::ostream& operator(std::ostream& os, const std::vector<My_Type>& v)
{
     // pick one of the other implementations for here...
    std::copy(std::istream_iterator<My_Type>(os), std::istream_iterator<My_Type>(),   
          std::back_inserter(v_Numbers));   
}

It's much nicer if you're well-mannered (;-p) enough to only overload your specific instantiation of vector (which requires My_Type be more than a typedef to say int, though it's not hard to create a templated class to create new types wrapping an arbitrary type). Otherwise, if someone else does the same elsewhere in your translation unit, the streaming could become ambiguous.

爺獨霸怡葒院 2024-10-09 09:04:40

我知道使用迭代器进行复制是最佳解决方案,但只是用 for_each 进行回答。

你可以这样做:

#include <vector>
#include <algorithm>
#include <locale>

int main() {
  using namespace std;
  locale::global(locale(""));
  wcout::imbue(locale());

  vector<int> vec{1000,2000,3000,4000,5000};
  for_each(vec.begin(), vec.end(), [](auto &x){wcout << x << endl;});
  
  return 0;
}

但是,对我来说,简单的 for 确实更具可读性......

#include <vector>
#include <locale>

int main() {
  using namespace std;
  locale::global(locale(""));
  wcout::imbue(locale());

  vector<int> vec{1000,2000,3000,4000,5000};
  for(auto &v: vec) {
    wcout << v << endl;
  }
  
  return 0;
}

I know the copy with the iterator is the optimal solution, but just to answer with for_each.

You could do:

#include <vector>
#include <algorithm>
#include <locale>

int main() {
  using namespace std;
  locale::global(locale(""));
  wcout::imbue(locale());

  vector<int> vec{1000,2000,3000,4000,5000};
  for_each(vec.begin(), vec.end(), [](auto &x){wcout << x << endl;});
  
  return 0;
}

But, for me, it's REALLY much more readable the simple for ...

#include <vector>
#include <locale>

int main() {
  using namespace std;
  locale::global(locale(""));
  wcout::imbue(locale());

  vector<int> vec{1000,2000,3000,4000,5000};
  for(auto &v: vec) {
    wcout << v << endl;
  }
  
  return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文