Luo Gu P1765 mobile phone _NOI Guide 2010 universal (10) and getline cin about some of the differences and some STL

A >> S cin:.
Cin >> is composed of two parts, and cin >>, where cin is the input stream istream an object class, belonging iostream library
and is operator >>: the operator function is: cin reading data from the left object and assign it to the right operand.
1 >> operator is the right side of the data type definitions, the right of different data types, overloaded. >> different
2 >> operator is left combination.
3:00 >> operator to read data from the cin object: If you have a space or line breaks, then the end of the read data, the previous space data is assigned to the right operand first after the valid data, the corresponding first symbol read, but also a space or line breaks
4. a >> operator can read only once (a) data, to read a plurality of data, plus >> operator only
two .getline ()
1. String object which is defined only by the object string (String object course << is overloaded)
2. ** getline encounter space does not end! ! ! That it can be read from a space object cin! ! ** Of course, experience is still wrap over, but he would read a line break, but after reading the following newline stop reading, then read the data (this time including newline) is assigned to the right of the operands (x, see below), when assigned, discarded line breaks! ! ! , That does not exist newline x to the
return value is the flow parameter getline cin >> and expressions, the return cin >>
3.getline is a function of the form:
** Summary: Use getline must use the string string defined and cin can not read spaces or line breaks. But getline unaffected
----------------
Disclaimer: This article is CSDN blogger "birate_ Little Life" original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/u014183456/article/details/88323575

 

Code 1

#include<bits/stdc++.h>
using namespace std;
int main(){
    int i,ans=0;
    string s;
    int num[26]={1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,4,1,2,3,1,2,3,4};
    getline(cin,s);                     //如果改成cin>>s;  则会出现错误
    for(i=0;i<s.length();i++){
        if(s[i]>='a'&&s[i]<='z'){
  ans+=num[s[i]-'a'];}
        if(s[i]==' '){
  ans++;}
    }
    cout<<ans;
    return 0;
}

 

 

Code 2

#include<bits/stdc++.h>
using namespace std;
int main()
{
 int ans=0;
 map<char,int>ma;      //定义map,把char类型转化成int               此处不能用string,因为string的+不是数字相加!!
    ma['a']=1;ma['b']=2;ma['c']=3;ma['d']=1;ma['e']=2;
    ma['f']=3;ma['g']=1;ma['h']=2;ma['i']=3;ma['j']=1;
    ma['k']=2;ma['l']=3;ma['m']=1;ma['n']=2;ma['o']=3;
    ma['p']=1;ma['q']=2;ma['r']=3;ma['s']=4;ma['t']=1;
    ma['u']=2;ma['v']=3;ma['w']=1;ma['x']=2;ma['y']=3;
 ma['z']=4;ma[' ']=1;
 string word;
    getline(cin,word);
    for(int i=0;i<word.length();i++)
    {
        ans=ans+ma[word[i]];
    }
    cout<<ans<<endl;
}

Guess you like

Origin www.cnblogs.com/QingyuYYYYY/p/11621664.html