`for_each` 没有像我预期的那样工作
`for_each` not working as I expect
我正在尝试函数式编程的一些功能,并尝试为向量中的每个对象调用一个成员函数。到目前为止,这是我的代码。
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;
}
这段代码很可能存在问题,但对我来说奇怪的是我 收到的两条错误消息。
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));
^
我正在使用 GCC 5.2.0 -std=c++14
进行编译。
for_each
在 <algorithm>
你可以做到
for_each(myVector.begin(), myVector.end(), [&] (const decltype(myVector.front())& a) { a.greet(); });
首先你忘了包含 header <algorithm>
。在这种情况下你不能使用 this
。
尝试以下方法
#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 ) );
}
程序输出为
Hello, Hello
Hello, World
Goodbye, Hello
Hello says hello!
World says hello!
Goodbye, World
Goodbye, Hello
如果在向量定义后包含以下调用
std::vector<Person> myVector;
myVector.reserve( 2 );
那么输出将是
Hello, Hello
Hello, World
Hello says hello!
World says hello!
Goodbye, World
Goodbye, Hello
即添加第二个元素时不会重新分配内存
我正在尝试函数式编程的一些功能,并尝试为向量中的每个对象调用一个成员函数。到目前为止,这是我的代码。
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;
}
这段代码很可能存在问题,但对我来说奇怪的是我 收到的两条错误消息。
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));
^
我正在使用 GCC 5.2.0 -std=c++14
进行编译。
for_each
在 <algorithm>
你可以做到
for_each(myVector.begin(), myVector.end(), [&] (const decltype(myVector.front())& a) { a.greet(); });
首先你忘了包含 header <algorithm>
。在这种情况下你不能使用 this
。
尝试以下方法
#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 ) );
}
程序输出为
Hello, Hello
Hello, World
Goodbye, Hello
Hello says hello!
World says hello!
Goodbye, World
Goodbye, Hello
如果在向量定义后包含以下调用
std::vector<Person> myVector;
myVector.reserve( 2 );
那么输出将是
Hello, Hello
Hello, World
Hello says hello!
World says hello!
Goodbye, World
Goodbye, Hello
即添加第二个元素时不会重新分配内存