#include<iostream>#include<stdio.h>#include<string.h>usingnamespacestd;intmain(){ string a; a.resize(100);//需要预先分配空间scanf("%s", &a[0]);printf("%s\n", a.c_str());return0; }
用printf输出 STL string类型数据总结 一.例子 #include <stdio.h> #include <string.h> using namespace std; int main() { string a; a[0]='a'; a[1]='/0'; printf("%s/n",a); system("pause"); } 出错: [Warning] cannot pass objects of non-POD type `struct std::string' through ...
printf只能输出C语言内置的数据,而string不是内置的,只是一个扩展的类,这样肯定是链接错误的。string不等于char*,&a代表的是这个字符串的存储地址,并不是指向字符串的首地址,aa 对象中包含一个指向"string"的指针, &aar得到的是这个对象的地址,不是"string"的地址。 printf输出string类型应如此操作! #include<ios...
18 swap(s1, s2); 19 cout<<"stringA: "<<s1<<"\t"<<"stringB: "<<s2<<endl; 20 // printf("stringA: %s\tstringB: %s\n", s1, s2); 21 22 exit(0); 23 } 使用printf输出string类型时候出现错误:warning: cannot pass objects of non-POD type 'struct std::string' through '...'...