c++ primer 第三章 标准库类型 string

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

int main()
{
   string s1,s2;
   getline(cin,s1);
   getline(cin,s2);

   if(s1.size() >= s2.size())
   {
       cout << s1;
   }
   else{
    cout<<s2;
   }
    return 0;
}

3.5

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

int main()
{
   string input,result="";
   while(cin>>input&& input!="#")
   {
       result += input+" ";
   }
   cout<< result;

    return 0;
}

std::toupper()的用法

c++API中提供的

#include <iostream>
#include <cctype>
#include <clocale>
 
int main()
{
    unsigned char c = '\xb8'; // the character ž in ISO-8859-15
                              // but ¸ (cedilla) in ISO-8859-1 
 
    std::setlocale(LC_ALL, "en_US.iso88591");
    std::cout << std::hex << std::showbase;
    std::cout << "in iso8859-1, toupper('0xb8') gives " << std::toupper(c) << '\n';
    std::setlocale(LC_ALL, "en_US.iso885915");
    std::cout << "in iso8859-15, toupper('0xb8') gives " << std::toupper(c) << '\n';
}
Output: 

in iso8859-1, toupper('0xb8') gives 0xb8
in iso8859-15, toupper('0xb8') gives 0xb4

自己写的例子

#include <iostream>
#include<string>
#include<cctype>

using namespace std;

int main()
{
    string s("hello wprld!!!");
    for(auto &c : s)
    {
        c = toupper(c);
    }
    cout <<s<<endl;
    return 0;
}

output

HELLO WORLD!!!

将第一个单词大写

#include <iostream>
#include<string>
#include<cctype>

using namespace std;

int main()
{
  string s ("some string");
  for(decltype(s.size()) index =0;
        index !=s.size() && !isspace(s[index]); ++index)
        {
            s[index] = toupper(s[index]);
        }
    cout << s<<endl;
    return 0;
}
output

SOME string

3.8

#include <iostream>
#include<string>
#include<cctype>

using namespace std;

int main()
{
 string s("12345");
 /*for(auto &c: s)
 {
     c = 'X';
 }
 cout <<s<<endl;
*/
decltype(s.size()) len = 0;
if(!s.empty())
{
    while( len < s.size())
{
        s[len] = 'X';
        len++;
}
}
cout << s<< endl;

    return 0;
}

用while写麻烦一些,用心时间来说,前者在0.022s左右,后者在0.025s,所以如果数量很大的话,用范围for要快一些

3.9

在gcc下和在VS2010编译器下测试都没有问题,不会报错

但是根据作者的理论,会出现不可预知错误,我猜是c++编译器给string付了默认的什么值

所以没有报错,但是没有输出有说明string是空的???搞不清

搞清了,string有默认初始化,所以在声明是没有提供初始化的值,编译器会将string对象初始化为空串,

所以没有报错,也没有输出!~2014、08、22

3.10

#include <iostream>
#include<string>
#include<cctype>

using namespace std;

int main()
{
 string  s = "sfdsdfwe$dasfa&(* HJGHGk  iyt 9uu08duwefrwerw ejhfriweh8&&xiasuhfc\
  wuieha7676tIHauy87^76U6%&%7hu&*&57<daeff.wefwefw,fw.gfdg.dgdt.h";
 if(!s.empty())
 {
        for(auto &c : s)
        {
            if(ispunct(c))
                c = ' ';
        }
 }
 cout << s<< endl;

    return 0;
}

3.11

合法,c为const char & 类型,截的图是codeblocks的









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值