string s1 = "abc"; // 初始化一个字符串 cout << s1.empty() << endl; // s 为空返回 true,否则返回 false cout << s1.size() << endl; // 返回 s 中字符个数,不包含空字符 cout << s1.length() << endl; // 作用同上 cout << s1[1] << endl; // 字符串本质是字符数组 cout <...
string str3;getline(cin, str3);cout << "str3 = " << str3 << endl;getline函数有两个参数:一个是输入流对象cin,另一个是保存字符串的string对象;它会一直读取输入流中的内容,直到遇到换行符为止,然后把所有内容保存到string对象中。所以现在可以完整读取一整行信息了。(3)使用get读取字符还有一种...
//字符串打印 :// ① 打印字符串 , cout 后的 << 后可以打印 字符串 , 也可以打印变量// ② 输出 cout << 字符串或变量1 << 字符串或变量2 ... << endl 可以拼接 输出信息cout<<"string_c : "<<string_c<<endl;cout<<"string_c_p : "<<string_c_p<<endl; 4.代码 : 代码语言:javascr...
std::string s = "hello world"; std::cout<<s<<std::endl; for (std::string::size_type ix = 0; ix != s.size(); ++ix) s[ix] = '*'; std::cout<<"Now s is:"<<s<<std::endl; std::cout<<"s's len is:"<<s.size()<<", s[12]="<<s[100]<<std::endl; return 0...
#include <string> int main() { std::string s = "hello world"; std::cout<<s<<std::endl; for (std::string::size_type ix = 0; ix != s.size(); ++ix) s[ix] = '*'; std::cout<<"Now s is:"<<s<<std::endl;
cout << x << endl; //打印20 cin >> zero; return 0; } /*将字符串的内容反转*/ #include <iostream> #include <string> void main() { using namespace std; cout << "输入一个单词: "; string word;//定义一个字符串变量word cin >> word; ...
string s1="123456789";// 将 string 转为 char*constchar*s2=s1.c_str();cout<<"s2 : "<<s2<<endl; 3、string 转为 char* - copy() 成员函数 std::string类的copy()成员函数 , 原型如下 : 代码语言:javascript 代码运行次数:0 运行
答c语言中string输出是以string 为对象输入忽略开头遇到的所有空字符,从第一个有效字符开始输入,直到遇到下一个空字符结束。如:string str;cin str;如果你输入的是 hello world cout str endl;那么输出的话结果是: hello。
String类的构造函数和析构函数如下: 代码实例: #include<iostream>#include<string>usingnamespacestd;intmain(){strings1;cout<<s1<<endl;//没有赋值输出为空strings2(10,'f');cout<<s2<<endl;//用10个f定义字符串s2,输出ffffffffffstrings3(s2);cout<<s3<<endl;//用s2定义上,将s3拷贝给s2,s2和s3...
strings(string(*)()); 接下来是一个相对冷的知识,首先很多人应该知道,在C和C++中,“函数”和“函数指针”是两个类型概念,它们并不等价: ~/test/cpp_test$ cat2.cpp#include<iostream>usingstd::cout,std::endl;intmain(){voidf();void(*pf)();cout<<typeid(f).name()<<endl;cout<<typeid(pf)....