Lambda function

Hi everybody ,
I try to copy data of a vector has an other vector


1
2
3
4
5
6
7
8
    string hello="thelifeisbeautiful";
     vector<char>life;
     life.resize(hello.size());
     std::copy(hello.begin(),hello.end(),life.begin());
     cout<< life.data()<< endl;

     std::function<int(vector<char>)>love=[](vector<char>loveagain)-> char{cout<<loveagain.data()<<endl;};
     std::copy(life.begin(),life.end().love.begin()); // copy the data buffer life in buffer love 

but the code don t work
std::copy(life.begin(),life.end().love.begin());

This line looks like you have used . somewhere you meant ,
thank's but don t work


1
2
3
4
5
6
7
8
   string hello="thelifeisbeautiful";
     vector<char>life;
     life.resize(hello.size());
     std::copy(hello.begin(),hello.end(),life.begin());
     cout<< life.data()<< endl;

     std::function<int(vector<char>)>love=[](vector<char>loveagain)-> char{cout<<loveagain.data()<<endl;};
     std::copy(life.begin(),life.end(),love.begin()); // copy the data buffer life in buffer love 
Since love is a function you cannot do that.

If you want to copy it to cout. See:

http://www.cplusplus.com/reference/iterator/ostream_iterator/
Are you trying to call the function love? Because this:

love.

is not how to call a function.
don t work

If that properly describes the issue, then
Yes.

is a sufficient answer?


Step 1. What does the compiler say?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>

int main()
{
  using std::string; using std::cout; using std::endl; using std::vector;
  string hello="thelifeisbeautiful";
  vector<char>life;
  life.resize(hello.size());
  std::copy(hello.begin(),hello.end(),life.begin());
  cout<< life.data()<< endl;
  std::function<int(vector<char>)> love = [](vector<char>loveagain)-> char {
    cout<<loveagain.data()<<endl;
  }; // warn
  std::copy( life.begin(), life.end().love.begin() ); // error 1
  std::copy( life.begin(), life.end(), love.begin() ); // error 2
}

 In lambda function:
17:3: warning: no return statement in function returning non-void [-Wreturn-type]

 In function 'int main()':
18:39: error: 'std::vector<char>::iterator' has no member named 'love'
19:45: error: 'class std::function<int(std::vector<char>)>' has no member named 'begin'


What is the "love" supposed to do?
Return an int, a char, or void?

What is the loop supposed to do?

Every element of life is a char. The love expects std::vector<char>.


love( life ); // ok


Would you explain why cout<< life.data()<< endl; does not detonate the moons of Jupiter? (Or does it?)
@chrichri

Is there a compelling reason to use std::vector<char> ? What is wrong with std::string ?
@ TheIdeasMan

i am a beginner in c++ and i want discover all face in c++
its a possible
In this example i want copy a buffer in a buffer with lambda function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <iterator>

int main()
{
    const std::string hello = "this life is beautiful" ;

    // closure object
    const auto love = [] ( const std::vector<char>& vec ) { std::cout << vec.data() << '\n' ; } ;

    // wrap the closure with a call wrapper
    std::function< void( const std::vector<char>& ) > love2 = love ;

    {
        // initialise the vector with the characters in the string
        std::vector<char> life( hello.begin(), hello.end() ) ;
        life.push_back(0) ; // add a null character at the end
                            // (we want to treat the contents of the vector as a c-style string)

        std::cout << life.data() << '\n' ;
        love(life) ;
        love2(life) ;
    }

    {
        // create a vector of adequate size and copy the characters in the string
        std::vector<char> life( hello.size() ) ;
        std::copy( hello.begin(), hello.end(), life.begin() ) ;
        life.push_back(0) ; // add a null character at the end

        std::cout << life.data() << '\n' ;
        love(life) ;
        love2(life) ;
    }

    {
        // create an empty vector and use a back insert iterator to push back the characters in the string
        std::vector<char> life ;
        std::copy( hello.begin(), hello.end(), std::back_inserter(life) ) ;
        life.push_back(0) ; // add a null character at the end

        std::cout << life.data() << '\n' ;
        love(life) ;
        love2(life) ;
    }

    {
        // create an empty vector and use a range based loop to push back the characters in the string
        std::vector<char> life ;
        for( char c : hello ) life.push_back(c) ;
        life.push_back(0) ; // add a null character at the end

        std::cout << life.data() << '\n' ;
        love(life) ;
        love2(life) ;
    }
}
@keskiverto

love return a char because i want to see the character.

the loop supposed copy the element of life to love.

life.data() to extract the data element of life to display.


@ TheIdeasMan

I'm beginner in c++ , i want know all face in c++ that is a reason i use vector <char> to understand how can it work.

I want in my example copy the element of life to love but i pass by lambda function to display the character which in the new buffer that means love.
@ JLBorges
thank's you
Topic archived. No new replies allowed.