对于代码,为什么错误,osteam_iterator是一个模板类,为什么没有匹配的构造函数用于'ostream_iterator'的初始化,请给予帮助,谢谢.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;
Run Code Online (Sandbox Code Playgroud) 我是一个 C++ 初学者,我周围有很多问题。我已经定义了 的!=运算符int_node,但是当我编译这段代码时,显示错误:
二进制表达式的无效操作数('int_node' 和 const 'int_node')
我使用的 IDE 是 xcode 4.6。
下面是我的所有代码
typedef struct int_node{
int val;
struct int_node *next;
} int_t;
template <typename Node>
struct node_wrap{
Node *ptr;
node_wrap(Node *p = 0) : ptr(p){}
Node &operator *() const {return *ptr;}
Node *operator->() const {return ptr;}
node_wrap &operator++() {ptr = ptr->next; return *this;}
node_wrap operator++(int) {node_wrap tmp = *this; ++*this; return tmp;}
bool operator == (const node_wrap &i) const {return ptr == i.ptr;}
bool …Run Code Online (Sandbox Code Playgroud) 以下"##"的含义是什么?
#define CC_SYNTHESIZE(varType, varName, funName)\
protected: varType varName;\
public: inline varType get##funName(void) const { return varName; }\
public: inline void set##funName(varType var){ varName = var; }
Run Code Online (Sandbox Code Playgroud) int main()
{
vector<int> vi;
vi.reserve(10);
// back_insert_iterator<vector<int> > iter(vi);
vector<int>::iterator iter = vi.begin();
*iter = 1;
++iter;
*iter = 2;
++iter;
*iter = 3;
back_insert_iterator<std::__1::vector<int> > iterb(vi);
back_inserter(vi) = 4;
back_inserter(vi) = 5;
vi.reserve(vi.size() * 2);
copy(vi.begin(), vi.end(), vi.end());
ostream_iterator<int> os(cout, " ");
copy(vi.begin(), vi.end(), os);
}
Run Code Online (Sandbox Code Playgroud)
问题1,为什么cout打印4 5,我预计会打印1 2 3 4 5 1 2 3 4 5?问题2,当我更换它时copy(vi.begin(), vi.end(), back_insert(vi)),它会打印4 5 4 5,为什么?