sprintf 编辑
字符串格式化命令,主要功能是把格式化的数据写入某个字符串中。sprintf 是个变参函数。
http://baike.baidu.com/link?url=uZyVRgepx8ACcd5qFD_CXK4C79Sb-Lxu5uqtrUdJJXS8yjqaNhOrnvcu4zVE84Opu8AiPrbhmwd7hGbRWPDpZ_
int main()
{
char buf[1100];
int a=10000;
sprintf(buf,"%d",a);
cout<<buf<<endl;
}
int main()
{
char buf[1100];
char a[100]="10000";
sprintf(buf,"%s",a);
cout<<buf<<endl;
}
此程序输出为10000
int main()
{
char buf[1100];
char a[100]="99";
int b=55;
sprintf(buf,"%s%d",a,b);
cout<<buf<<endl;
}
此程序输出为9955
这样sprintf的功能就很明了了。把确定类型值按照输入类型,输入到字符串中。
https://vjudge.net/problem/UVA-10815
isalpha:一种函数:判断字符ch是否为英文字母,若为英文字母,返回非0(小写字母为2,大写字母为1)。若不是字母,返回0。在标准c中相当于使用“ isupper( ch ) || islower( ch ) ”做测试,
用法
C++ cctype ctype.h
tolower :
tolower是一种函数,功能是把字母字符转换成小写。
s[i]=tolower(s[i]);
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string str= "THIS IS A STRING";
for (int i=0; i <str.size(); i++)
str[i] = tolower(str[i]);
cout<<str<<endl;
return 0;
}
stringsteam ss;
int i;
string s;
stringstream ss(s);
ss << i;
cout <<ss.str();
读取str中的单字,比如hello world ,就会读取hello和world。
创建存储str的副本的 stringstream 对象,其中str是 string 类型的对象
iostream标准库支持内存中的输入/输出,只要将流与存储在程序内存中的string对象捆绑起来即可。此时,可使用iostream输入和输出操作符读写这个string对象。标准库定义了三种类型的字符串流:
1.istringstream,由istream派生而来,提供读string的功能。
2.ostringstream,由ostream派生而来,提供写string的功能。
3.stringstream,由iostream派生而来,提供读写string的功能。
作为数字和字符串之间的相互转化
#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <algorithm>
#include <string>
#include <set>
#include <sstream>
using namespace std;
int main()
{
string name;
int age = 27;
stringstream os;
os << age;
os >> name;
// name:27
cout<<name<<endl;
}
输出结果为27的字符串型
用法;把字符串输入到ss里面
int main()
{
stringstream ss_stream;
ss_stream << "字符串一" << endl;
ss_stream << "字符串二" << endl;
ss_stream << "字符串三" << endl;
ss_stream << "字符串四" << endl;
ss_stream << "字符串五" << endl;
char buffer[100];
while ( ss_stream.getline(buffer, sizeof(buffer)))
{
printf("msg=%s\n", buffer);
}
}
注意重复使用同一个stringstream对象时要 先继续清空,而清空很容易想到是clear方法,而在stringstream中这个方法实际上是清空stringstream的状态(比如出错等),真 正清空内容需要使用.str(“”)方法。
组合使用 是完全清空
关于stringstream 的对象完全清空方法
是使用 claer
和,str 组合使用 才可以重复使用。
void cle(stringstream &s)
{
s.clear();
s.str("");
}
附加蓝桥杯算法训练 P1003 代码
题目链接: http://lx.lanqiao.cn/problem.page?gpid=T365
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <algorithm>
#include <string>
#include <set>
#include <sstream>
using namespace std;
map<string ,int > d;
map<string ,int > p;
void cle(stringstream &s)
{
s.clear();
s.str("");
}
int main()
{
string s,sr;
getline(cin,s);
string buf;
stringstream ss(s);
while(ss>>buf)
{
int x=strlen(&buf[0]);
string c=buf;
//cout<<x<<endl;
sort(&buf[0],&buf[0]+x);
//cout<<buf<<" z"<<endl;
d[buf]=1;
}
cle(ss);
getline(cin,sr);
ss<<sr;
while(ss>>buf)
{
int x=strlen(&buf[0]);
sort(&buf[0],&buf[0]+x);
//cout<<buf<<endl;
if(d[buf])
{
p[buf]=1;
//cout<<buf<<endl;
}
}
cle(ss);
ss<<s;
while(ss>>buf)
{
int x=strlen(&buf[0]);
string c=buf;
//cout<<x<<endl;
sort(&buf[0],&buf[0]+x);
if(p[buf]) cout<<c<<' ';
}cout<<endl;
}