zoukankan      html  css  js  c++  java
  • 刷题遇到的各种 之 字符串

    一、C++ strcpy字符串赋值函数

    strcpy(Cstring, value);

    Cstring 是接收值的变量的名称,而 value 则是字符串常数或另一个 C 字符串变量的名称

    #include <iostream>
    #include<string.h>
    using namespace std;
    
    int main()
    {
        const int SIZE = 12;
        char name1[SIZE], name2[SIZE];
        strcpy(name1, "Sebastian");
        cout << "name1:" << name1 << endl;
        strcpy(name2, name1);    //将  name1 赋值给 name2 
        cout << "name2:" << name2 << endl;
        return 0;
    }

    二、C++ 其余常见字符串处理函数:

    • 1. append() -- 在字符串的末尾添加字符
    • 2. find() -- 在字符串中查找字符串
    • 4. insert() -- 插入字符
    • 5. length() -- 返回字符串的长度
    • 6. replace() -- 替换字符串
    • 7. substr() -- 返回某个子字符串

    示例:

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        //定义一个string类对象
        string http = "www.runoob.com";
    
       //打印字符串长度
       cout<<http.length()<<endl;
    
        //拼接
        http.append("/C++");
        cout<<http<<endl; //打印结果为:www.runoob.com/C++
    
        //删除
        int pos = http.find("/C++"); //查找"C++"在字符串中的位置
        cout<<pos<<endl;
        http.replace(pos, 4, "");   //从位置pos开始,之后的4个字符替换为空,即删除
        cout<<http<<endl;
    
        //找子串runoob
        int first = http.find_first_of("."); //从头开始寻找字符'.'的位置
        int last = http.find_last_of(".");   //从尾开始寻找字符'.'的位置
        cout<<http.substr(first+1, last-first-1)<<endl; //提取"runoob"子串并打印
    
        return 0;
    }

    三、stringstream常见用法介绍

    <sstream> 定义了三个类:istringstream、ostringstream 和 stringstream,分别用来进行流的输入、输出和输入输出操作

    <sstream> 主要用来进行数据类型转换,由于 <sstream> 使用 string 对象来代替字符数组(snprintf方式),就避免缓冲区溢出的危险;而且,因为传入参数和目标对象的类型会被自动推导出来,所以不存在错误的格式化符的问题。简单说,相比c库的数据类型转换而言,<sstream> 更加安全、自动和直接

    (1)数据类型转换

    #include <string>
    
    #include <sstream>
    
    #include <iostream>
    
    #include <stdio.h>
    
     
    
    using namespace std;
    
     
    
    int main()
    
    {
    
        stringstream sstream;
    
        string strResult;
    
        int nValue = 1000;
    
     
    
        // 将int类型的值放入输入流中
    
        sstream << nValue;
    
        // 从sstream中抽取前面插入的int类型的值,赋给string类型
    
        sstream >> strResult;
    
     
    
        cout << "[cout]strResult is: " << strResult << endl;
    
        printf("[printf]strResult is: %s
    ", strResult.c_str());
    
     
    
        return 0;
    
    }

    (2)多个字符串拼接

    在 stringstream 中存放多个字符串,实现多个字符串拼接的目的(其实完全可以使用 string 类实现),同时,介绍 stringstream 的清空方法

    #include <string>
    #include <sstream>
    #include <iostream>
    using namespace std;
    int main()
    {
        stringstream sstream;
        // 将多个字符串放入 sstream 中
        sstream << "first" << " " << "string,";
        sstream << " second string";
        cout << "strResult is: " << sstream.str() << endl;
    
        // 清空 sstream
        sstream.str("");
        sstream << "third string";
        cout << "After clear, strResult is: " << sstream.str() << endl;
        return 0;
    
    }

    1.  可以使用 str() 方法,将 stringstream 类型转换为 string 类型
    2.  可以将多个字符串放入 stringstream 中,实现字符串的拼接目的;
    3.  如果想清空 stringstream,必须使用 sstream.str(""); 方式;
    4. clear() 方法适用于进行多次数据类型转换的场景,见链接中的示例

    详情可戳上方的链接

    四、C++ isalpha、isalnum、islower、isupper

    这些函数都在<cctype>(即C语言中的<ctype.h>)的头文件里面

    总的来说:

    • isalpha (字母,包括大写、小写)
    • islower(小写字母)
    • isupper(大写字母)
    • isalnum(字母大写小写+数字)
    • isblank(space和 )
    • isspace(space、 、 、 )
  • 相关阅读:
    BZOJ 1040 (ZJOI 2008) 骑士
    BZOJ 1037 (ZJOI 2008) 生日聚会
    ZJOI 2006 物流运输 bzoj1003
    ZJOI 2006 物流运输 bzoj1003
    NOI2001 炮兵阵地 洛谷2704
    NOI2001 炮兵阵地 洛谷2704
    JLOI 2013 卡牌游戏 bzoj3191
    JLOI 2013 卡牌游戏 bzoj3191
    Noip 2012 day2t1 同余方程
    bzoj 1191 [HNOI2006]超级英雄Hero——二分图匹配
  • 原文地址:https://www.cnblogs.com/expedition/p/11616279.html
Copyright © 2011-2022 走看看