64,654
社区成员
发帖
与我相关
我的任务
分享
char * a="hello";
//下面调用的都是ostream & operator<<(unsigned &)函数
cout<<(unsigned)a<<endl;//输出字符串"hello"首地址(十进制)
cout<<hex<<(unsigned)a<<endl;//输出字符串"hello"首地址(十六进制)
cout<<oct<<(unsigned)a<<endl<<;//输出字符串"hello"首地址(八进制)
vector<char*> *vec=new vector<char*>(1, a);
vector构造函数
default (1)
explicit vector (const allocator_type& = allocator_type());
fill (2)
explicit vector (size_type n, const value_type& val = value_type(),
const allocator_type& = allocator_type());
range (3)
template <class InputIterator>
vector (InputIterator first, InputIterator last,
const allocator_type& = allocator_type());
copy (4)
vector (const vector& x);
vector<char*> *vec=new vector<char*>(a);
需要看看定义类模板的代码才能帮你判断
第二个问题,C++语言对运算符“<<”进行了重载
char * a="hello";
cout <<a<<endl;\\调用函数 ostream & operator<<(char *),输出a指向的字符串"hello"
cout <<*a<<endl;\\调用函数 ostream & operator<<(char &),输出字符(*a)的值,即'h'
cout<<x;
编译器将根据参数x的类型调用相应的重载函数
若x为char*型,下面的语句将调用ostream & operator<<(char *)函数,输出x指向的字符和存储在该字符后面的所有字符,直到遇到'\0'结束。char* x = "hello";
cout<<x;
又如
char* x = "hello";
unsigned a = (unsigned)x;
cout<<a;
上面的语句先将指针强制转换成unsigned int类型,然后调用ostream & operator<<(unsigned &)函数,输出变量a的值