c++ - No matching constructor for initalization of 'ostream_iterator<int>' -


for code, why error, osteam_iterator template class ,why no matching constructor initalization of 'ostream_iterator', please give , thank you. define ostream_iterator template > class _libcpp_visible ostream_iterator

int main(int argc, const char * argv[]) {     vector<int> sentence1;     sentence1.reserve(5);// 设置每次分配内存的大小      sentence1.push_back(1);     sentence1.push_back(2);     sentence1.push_back(3);     sentence1.push_back(4);     sentence1.push_back(5);      int c = 5;      copy(sentence1.begin(), sentence1.end(), ostream_iterator<int>(cout, 1));     cout << endl; 

the ostream_iterator class definition looks like:

template< class t,   class chart = char,   class traits = std::char_traits<chart>> class ostream_iterator /*...*/ 

whereas respective constructor declared as:

ostream_iterator(ostream_type& buffer, const chart* delim) 

since second template argument of ostream_iterator required of character type cannot replace int.

if ommit second template parameter can plug in string literal of type char const *:

std::copy(sentence1.begin(), sentence1.end(), std::ostream_iterator<int>(cout, ",")); 

if c++11 available then

int c = 5; ( auto v : sentence1 ) std::cout << v << c; 

is way of doing deserve , might suitable, too. advantage is, operator<< more flexible argument of type "pointer char type".


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -