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
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
#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?)
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <iterator>
int main()
{
const std::string hello = "this life is beautiful" ;
// closure object
constauto 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) ;
}
}