ฉันกำลังอ่านการอ้างอิง C++ และเจอฟังก์ชัน std::plus พร้อมตัวอย่าง ซึ่งค่อนข้างตรงไปตรงมา เพียงเพิ่ม lhs และ rhs รหัสคือ:
#include <functional>
#include <iostream>
int main()
{
std::string a = "Hello ";
const char* b = "world";
std::cout << std::plus<>{}(a, b) << '\n';
}
เอาท์พุต: สวัสดีชาวโลก
ฉันเปลี่ยนมันเป็น
#include <functional>
#include <iostream>
int main()
{
int a = 5;
int b = 1;
std::cout << std::plus<int>{}(a, b) << '\n';
}
เอาท์พุต : 6
ตอนนี้ฉันทำ
foo vector = 10 20 30 40 50
bar vector = 11 21 31 41 51
ฉันโทรไป:
std::transform (foo.begin(), foo.end(), bar.begin(), foo.begin(), std::plus<int>());
และมันให้ 21 41 61 81 101 ซึ่งฉันเข้าใจว่ามันรวมทั้ง foo และ bar แต่มันถูกส่งผ่านไปยัง std::plus function ได้อย่างไร
std::transform
ได้อย่างไร en.cppreference.com/w/cpp/algorithm/transform - person 5gon12eder   schedule 23.02.2016