电话号码查询(getline函数)

问题:
输入是某电话公司的若干客户姓名及电话号码,中间用逗号分隔,然后是若干要查询的客户姓名,输出是这些查询的客户姓名及其电话。

输入:
第一行是一个正整数n(不超过100000),表示某电话公式的客户个数,然后是n个客户,每个客户占一行,由一个字符串构成的姓名和一个电话号码组成,中间用逗号分隔,每个姓名的长度不超过45。

接下来是一个正整数m,然后是m个需要查询的客户姓名,每个姓名占一行。

输出:
对于每个查询的客户,如果该客户是电话公式的客户,则显示其姓名和电话号码,如果不存在,则显示姓名和‘No',中间用‘:’分隔。每个输出占一行。

输入:

4
Bob,34778654
Ana,567894833
Alice,3456789
Gates,6893838
5
Alice
bob
Gate
Bob
Ana

输出

Alice:3456789
bob:No
Gate:No
Bob:34778654
Ana:567894833
 

你需要注意的是

getline函数的用法,可以在一段输入流中利用分隔符分割字符串,注意是将流中的内容放入到一个字符串中,加上分隔符则是将分割符号前(不包括分割符号)的内容全部放入一个string的类型中。以下是c++11中对于getline的定义,c++98中还并没有包含分割字符串的输入方式:

(1)	
istream& getline (istream&  is, string& str, char delim);
istream& getline (istream&& is, string& str, char delim);
(2)	
istream& getline (istream&  is, string& str);
istream& getline (istream&& is, string& str);

你可能还需要注意其他的两点:

1)getline函数会将换行符一并存入和读取,因此你需要做好防止:

我们首先来看带分隔符的getline(1)函数的处理情况:

#include<iostream>
#include<string>
using namespace std;

int main(){
    string str1;
    string str2;
    cin >> str1;
    getline(cin, str2, ',');
    cout << str1 << "  No  \'\\n\' readed";
    cout << str2 << "  But this time \'\\n\' readed ";
}

Input
str1
str2,123456

Output
str1  No  '\n' readed
str2  But this time '\n' readed

然后再来看不带分隔符的getline(2)函数的处理情况: 

#include<iostream>
#include<string>
using namespace std;

int main(){
    string str1;
    string str2;
    cin >> str1;
    getline(cin, str2);
    cout << str1 << "  No  \'\\n\' readed";
    cout << str2 << "  But this time \'\\n\' readed ";
}

Input

str1

Output
str1  No  '\n' readed  But this time '\n' readed  

我们甚至没有来得及输入str2以及后面的内容

由此可见str2已经为空,这样你就会更加明白getline函数在c++中的实现机制,想想上一段代码中是不是也没有输出分隔符号呢?由此可见c++11中的函数相比c++98的改进只不过是做了很香的(同时也很小的)改变一个函数接口的动作。你也就不会疑惑为什么我们会在下面的代码中用到一些修正了。

2)getline函数是在c++ <string> 头文件中,使用时应当引用

3)你还可以尝试通过fgets,等函数阅读一行的数据,同样他们也会到分隔符停止并且不会将分隔符保存进类型中。

时间的冗余,

你需要尽量避免使用<vector>,他有可能会使你的代码运行超时:

#include<iostream>
#include<string>
#include<vector>
#include<sstream>
using namespace std;

int main(){
    int T;
    cin >>T;
    vector<string> name;
    vector<string> number;
    int counter = 0;
    while(T--){
        string name_, number_;
        cin >> name_ ;
        int a  = name_.find(",",0);
        for(int i=a+1; i < (int)name_.size(); ++ i){
            number_ += name_[i];
        }
        name_.erase(name_.begin()+a,name_.end());
        name.push_back(name_);
        number.push_back(number_);
        ++ counter;
    }
    /*for(int i=0; i < (int)name.size(); ++ i){
        cout << name[i] << endl;
    }
    for(int i=0; i < (int)number.size(); ++ i){
        cout << number[i] << endl;
    }*/
    int q=0;
    cin >>  q;
    while(q--){
        string name_;
        cin >> name_;
        int i = 0;
        int flag = 1;
        while( i < counter){  // size of the vector
            //cout << "name[i]*****" << name[i] << "name_******" << name_ << (name[i] == name_) ;
            if(name[i] == name_){
                cout << name_ << ":" << number[i] <<endl;
                flag = 0;
                break;
            }
            ++ i;
        }
        if(flag){
            cout << name_ << ":" << "No\n" ;
        }
    }
}

下面放出可行的两份答案吧:

c++   standard answer: 

#include <iostream>
#include <map>
#include <string>
#include <sstream>
using namespace std;

int main()
{
    int len;
    cin >> len;
    map<string, string> m;
    for (int i = 0; i < len; i++){
        string name_, number_;
        string item;
        cin>>item;
        stringstream ss(item);
        getline(ss,name_,',');
        getline(ss,number_,',');
        //m.insert(pair<string,string>(name,phone));
        m[name_] = number_;//这两种插入方式都可以,insert一般用于多值插入情况
    }
    cin >> len;
    for (int i = 0; i < len; i++){
        string name;
        cin >> name;
        if (m.find(name)==m.end())
            cout << name << ":No" << endl;
        else
            cout << name << ":" << m[name] << endl;
    }
    return 0;
}

or:

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main()
{
    int len;
    cin >> len;
    map<string, string> m;
    for (int i = 0; i < len; i++){
        cin.get();//前面cin后,最后的'\n'还在,需要手动去掉。
        string name;
        getline(cin, name, ',');//以','为分隔符,将姓名提取出来
        string phone;
        cin >> phone;
        //m.insert(pair<string,string>(name,phone));
        m[name] = phone;//这两种插入方式都可以,insert一般用于多值插入情况
    }
    cin >> len;
    for (int i = 0; i < len; i++){
        string name;
        cin >> name;
        if (m.find(name)==m.end())
            cout << name << ":No" << endl;
        else
            cout << name << ":" << m[name] << endl;
    }
    return 0;
}
//-----------------------------------------------------------
/*转载自CSDN博客https://blog.csdn.net/Stark_JC/article/details/82080799?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~aggregatepage~first_rank_ecpm_v1~rank_v31_ecpm-1-82080799.pc_agg_new_rank&utm_term=getline%E5%88%86%E9%9A%94%E7%AC%A6&spm=1000.2123.3001.4430*/

 其他博客传送门:C++ 使用 stringstream与getline()分割字符串_Franden的博客-CSDN博客_getline分割字符串

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值