include<iostream>#include<string>using namespace std;int main(){string str;int i=0;char c;while(cin>>c){if(c==' ') // 这样就行了{}//去掉多余的空格else if(c>='A' && c<= 'Z'){str += c+32;// i++;}else if (c>='a'&&c<='z'){str += c-32; // i++...
std::string line; // empty string while(std::getline(std::cin, line)) { // read line at time until end-of-file std::cout << line << std::endl; // write s to the output } return 0; } Name: getline 这个函数接受两个參数:一个输入流对象和一个 string 对象。getline 函数从输入流...
1、单个单词能够直接用std::cin,由于:std::cin读取并忽略开头全部的空白字符(如空格,换行符,制表符)。读取字符直至再次遇到空白字符,读取终止。 所以cin仅仅能读取单个单词。显然能够多次使用cin来获取多个单词; 2、多个单词使用函数std::getline(std::cin, s)请看以下代码: #include <iostream> #include <strin...
string str_3("hello world"); //直接初始化 string str_4{ "hello world" }; //直接初始化 //可以使用上面任意一种来初始化string对象,并且string字符串是不保存'\0'的,string对象有自己的成员函数 //用来记录字符串大小,所以不变判断字符串结尾 //赋值 string str; char a[20] = { "abcde" }; s...
while (cin >> n) { if (n == 0) break; string *p = new string[n]; for (int i = 0; i < n; ++i) { cin >> p[i]; } sort(p, p + n); for (int i = 0; i < n; ++i) { cout << p[i] << endl; } }
出现这个错误一般是操作数的类型与操作符所要求的不匹配,比如(string类型需要<string>头文件): 1.使用系统的类时出错,或者尝试对不正确的类型进行运算符操作 比如: string a="abc"; cout<<3+a; //将一个整数与一个string类型的变量相加,这是+运算不允许的 2.在使用自己定义的类时,尝试使用系统默认的运算符...
#include <bits/stdc++.h>using namespace std;int main(){string s,s1,s2;char c;//过滤,读入字符串s,s1,s2while(cin >> c && c != ',') s += c;while(cin >> c && c != ',') s1 += c;while(cin >> c ) s2 += c;//判断字符串s的长度是否包含了字符串s1,s2if(s1.size()...
cin.ignore(); //加上这一句,忽略回车符号#include <string.h>#include <iostream>using namespace std;main(){int flag=1; while(flag) { char b[100]; cout<<"输入要执行的SQL语句:"<<endl; cin.getline(b,100); cout<<"继续?"<<endl; cin>>flag; cin....
cin >> f; cout << "浮点型变量f = "<< f << endl; //3、字符型 int ch = 'a'; cout << "请给字符型变量ch赋值:"<<endl; cin >> ch; cout << "字符型变量ch = "<< ch << endl; //4、字符串型 #include<string> //记得加上字符串头文件 ...