#include<string>
#include<iostream>
using namespace std;
main()
{
string s("12345asdf");
string a=s.substr(0,5); //获得字符串s中 从第0位开始的长度为5的字符串//默认时的长度为从开始位置到尾
cout<<a<<endl;
}
输出结果为:
12345
#include<string>
#include<iostream>
using namespace std;
main()
{
string s("12345asdf");
string a=s.substr(0,5); //获得字符串s中 从第0位开始的长度为5的字符串//默认时的长度为从开始位置到尾
cout<<a<<endl;
}
输出结果为:
12345
1 函数 substr
2 格式 substr(字符串,截取开始位置,截取长度)
3 返回值 : 按照开始位置和截取长度得到的字符串
#include <bits/stdc++.h>
using namespace std;
string st,S;
map <string,string> a;
int main()
{
while (1)
{
getline(cin,st);
if (st=="") break;
int pos=st.find(" ");
string s1=st.substr(0,pos);
string s2=st.substr(pos+1);
a[s2]=s1;
}
while (getline(cin,S))
{
if (a[S]!="")cout<<a[S]<<endl;
else cout<<"eh"<<endl;
}
return 0;
}