`for_each`不工作,我期待 [英] `for_each` not working as I expect

查看:166
本文介绍了`for_each`不工作,我期待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在和函数式编程的一些特性,我试图调用一个成员函数向量中的每个对象。这是我的code为止。

  

emplace.cc

 的#include<的iostream>
#包括<载体>
#包括<字符串>
的#include<功能>

类Person {
   上市:
      人(标准::字符串名称,为size_t岁):_name(名称),_age(年龄){性病::法院<< 你好,<<的getName()&其中;&其中;的std :: ENDL; }
      〜人(){性病::法院<< 再见,<<的getName()&其中;&其中;的std :: ENDL; }
      无效迎接(){性病::法院<<的getName()&其中;&其中;说:你好! <<的std :: ENDL; }
      标准::字符串的getName()const的{返回_name; }
   私人:
      标准::字符串_name;
      为size_t _age;

};

诠释的main()
{
   的std ::矢量<人> myVector;
   myVector.emplace_back(你好,21);
   myVector.emplace_back(世界,20);

   的std :: for_each的(myVector.begin(),myVector.end()的std :: bind1st(的std :: mem_fun(安培;人::迎接),本));
   返回0;
}
 

这很可能是有问题,这个code,但奇怪的是,我的两个错误消息,我的(上午)的获取。

  emplace.cc:24:4:错误:'的for_each不是性病的一员
    的std :: for_each的(myVector.begin(),myVector.end()的std :: bind1st(的std :: mem_fun(安培;人::迎接),本));
    ^
emplace.cc:24:95:错误:无效使用'这个'非成员函数
    的std :: for_each的(myVector.begin(),myVector.end()的std :: bind1st(的std :: mem_fun(安培;人::迎接),本));
                                                                                               ^
 

我编译与 -std = C ++ 14 使用GCC 5.2.0。

解决方案

首先,你忘了,包括标题的<算法> 。你可以不使用在这种情况下。

请尝试以下

 的#include<的iostream>
#包括<载体>
#包括<算法>
的#include<功能>


类Person {
   上市:
      人(标准::字符串名称,为size_t岁):_name(名称),_age(年龄){性病::法院<< 你好,<<的getName()&其中;&其中;的std :: ENDL; }
      〜人(){性病::法院<< 再见,<<的getName()&其中;&其中;的std :: ENDL; }
      无效迎接(){性病::法院<<的getName()&其中;&其中;说:你好! <<的std :: ENDL; }
      标准::字符串的getName()const的{返回_name; }
   私人:
      标准::字符串_name;
      为size_t _age;

};

诠释的main()
{
   的std ::矢量<人> myVector;
   myVector.emplace_back(你好,21);
   myVector.emplace_back(世界,20);

   的std :: for_each的(myVector.begin(),myVector.end()的std ::的mem_fn(&安培;人::迎接));
}
 

该程序的输出为

 你好,你好
你好,世界
再见,你好
您好问好!
世界说你好!
再见,世界
再见,你好
 

如果您包括矢量定义后,下面的调用

 的std ::矢量<人> myVector;
myVector.reserve(2);
 

那么输出将是

 你好,你好
你好,世界
您好问好!
世界说你好!
再见,世界
再见,你好
 

这是当第二元素被添加的存储器将不被重新分配。

I'm playing with some features of functional programming and am trying to call a member function for each object in a vector. Here's my code so far.

emplace.cc

#include <iostream>
#include <vector>
#include <string>
#include <functional>

class Person {
   public:
      Person(std::string name, size_t age) : _name(name), _age(age) { std::cout << "Hello, " << getName() << std::endl; }
      ~Person() { std::cout << "Goodbye, "<< getName() << std::endl; }
      void greet() { std::cout << getName() << " says hello!" << std::endl; }
      std::string getName() const { return _name; }
   private:
      std::string _name;
      size_t _age;

};

int main()
{
   std::vector<Person> myVector;
   myVector.emplace_back("Hello", 21);
   myVector.emplace_back("World", 20);

   std::for_each(myVector.begin(), myVector.end(), std::bind1st(std::mem_fun(&Person::greet), this));
   return 0;
}

It's quite likely that there are problems with this code, but what is strange for me is the two error messages that I am getting.

emplace.cc:24:4: error: 'for_each' is not a member of 'std'
    std::for_each(myVector.begin(), myVector.end(), std::bind1st(std::mem_fun(&Person::greet), this));
    ^
emplace.cc:24:95: error: invalid use of 'this' in non-member function
    std::for_each(myVector.begin(), myVector.end(), std::bind1st(std::mem_fun(&Person::greet), this));
                                                                                               ^

I'm compiling with -std=c++14 using GCC 5.2.0.

解决方案

First of all you forgot to include header <algorithm>. And you may not use this in this context.

Try the following

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>


class Person {
   public:
      Person(std::string name, size_t age) : _name(name), _age(age) { std::cout << "Hello, " << getName() << std::endl; }
      ~Person() { std::cout << "Goodbye, "<< getName() << std::endl; }
      void greet() { std::cout << getName() << " says hello!" << std::endl; }
      std::string getName() const { return _name; }
   private:
      std::string _name;
      size_t _age;

};

int main()
{
   std::vector<Person> myVector;
   myVector.emplace_back("Hello", 21);
   myVector.emplace_back("World", 20);

   std::for_each(myVector.begin(), myVector.end(), std::mem_fn( &Person::greet ) );
}

The program output is

Hello, Hello
Hello, World
Goodbye, Hello
Hello says hello!
World says hello!
Goodbye, World
Goodbye, Hello

If you include the following call after the vector definition

std::vector<Person> myVector;
myVector.reserve( 2 );    

then the output will be

Hello, Hello
Hello, World
Hello says hello!
World says hello!
Goodbye, World
Goodbye, Hello

that is the memory will not be reallocated when the second element is added.

这篇关于`for_each`不工作,我期待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆