首页 > 编程语言 >C++精解【2】

C++精解【2】

时间:2024-06-22 20:00:25浏览次数:14  
标签:精解 const string C++ char str strRx include

文章目录

数据类型

基本数据类型

在C++中,基本数据类型包括整数类型、浮点类型和字符类型。整数类型包括short、int、long和char,浮点类型包括float和double。

以下是各种基本数据类型的大小和范围:

整数类型:

short:2字节

int:通常是4字节

long:在64位系统上是8字节,32位系统是4字节

char:通常是1字节,可能是有符号的,也可能是无符号的

浮点类型:

float:4字节

double:8字节

字符类型:

char:通常是1字节,可以是有符号的也可以是无符号的
short shortVar = 123;
int intVar = 12345678;
long longVar = 123456789012345;
 
float floatVar = 1.23f;
double doubleVar = 1.23456789;
 
char charVar = 'A';

以上内容引用自AI生成内容:AI自动生成内容

更多类型

1、bool型(布尔型):true或false

bool isOk=true;
bool isBlue=false;

2、string
C++string 是C++中的字符串。 字符串对象是一种特殊类型的容器,专门设计来操作的字符序列。 不像传统的c-strings,只是在数组中的一个字符序列,我们称之为字符数组,而C + +字符串对象属于一个类,这个类有很多内置的特点,在操作方式,更直观,另外还有很多有用的成员函数。
string 的定义为:typedef basic_string string;
更多内容请见string,百度百科

sizeof

在 C++ 语言中,sizeof() 是一个判断数据类型或者表达式长度的运算符。

cout<<sizeof(int)<<endl;

正则表达式

smatch 对应于 string,wsmatch 对应于 wstring,cmatch 对应于 char 或 wcmatch 对应于 wchar_t*。*

单次匹配

#include <iostream>
#include <regex>
#include <string>

int main() {
    using namespace std;
    const char *str = "abce12389iiu4645ppp";
    const char *first=str;
    const char *last=str+strlen(str);
    regex strRx(R"(\d+)");
    cmatch match;

    bool found = regex_search(first, last, match, strRx);
    if (found)
        cout <<"find the number:"<<match.str() << endl;
    return 0;
}

find the number:12389

Process returned 0 (0x0)   execution time : 0.045 s
Press any key to continue.

多次匹配

#include <iostream>
#include <regex>
#include <string>
using namespace std;
int main() {
    const char *str = "abce12389iiu4645ppp";
    const char *first=str;
    const char *last=str+strlen(str);
    typedef regex_iterator<const char *> StrIter;
    StrIter::regex_type strRx(R"(\d+)");
    StrIter next(first, last, strRx);
    StrIter end;

    for (; next != end; ++next)
        std::cout << "find number: " << next->str() << std::endl;
}

find number: 12389
find number: 4645

Process returned 0 (0x0)   execution time : 0.293 s
Press any key to continue.

组匹配

find the str:12389iiu4645
find the str:12389
find the str:iiu
find the str:4645

Process returned 0 (0x0)   execution time : 0.287 s
Press any key to continue.

#include <iostream>
#include <regex>
#include <string>

int main() {
    using namespace std;
    const char *str = "abce12389iiu4645ppp";
    const char *first=str;
    const char *last=str+strlen(str);
    regex strRx(R"((\d+)(\D+)(\d+))");
    cmatch matchs;

    bool found = regex_search(first, last, matchs, strRx);
    if (found){
        cout <<"find the str:"<<matchs[0].str() << endl;
        cout <<"find the str:"<<matchs[1].str() << endl;
        cout <<"find the str:"<<matchs[2].str() << endl;
        cout <<"find the str:"<<matchs[3].str() << endl;
    }
    return 0;
}

字符串的匹配

使用smatch
smatch 对应于 string,wsmatch 对应于 wstring,cmatch 对应于 char 或 wcmatch 对应于 wchar_t*。*

#include <iostream>
#include <regex>
#include <string>

int main() {
    using namespace std;
    string str = "abce12389iiu4645ppp";
    regex strRx(R"((\d+)(\D+)(\d+))");
    smatch matchs;

    bool found = regex_search(str, matchs, strRx);
    if (found){
        cout <<"find the str:"<<matchs[0].str() << endl;
        cout <<"find the str:"<<matchs[1].str() << endl;
        cout <<"find the str:"<<matchs[2].str() << endl;
        cout <<"find the str:"<<matchs[3].str() << endl;
    }
    return 0;
}

标签:精解,const,string,C++,char,str,strRx,include
From: https://blog.csdn.net/sakura_sea/article/details/139873719

相关文章

  • C++设计模式 之 Monostate模式
    目录标题第一章:Monostate模式简介什么是Monostate模式?Monostate模式的工作原理使用Monostate模式的优势适用场景第二章:实现Monostate模式基本结构构造函数和析构函数使用案例总结第三章:Monostate模式的优缺点优点缺点Monostate模式与单例模式的比较第四章:Monostate模......
  • c++中string的用法
    STL的简介一.什么是STL二.STL的六大组件2.1仿函数2.2空间配置器2.3算法2.4迭代器2.5容器2.6配置器三.string类3.1string类3.2string类的常用接口说明代码示例运行结果3.3string类对象的容量操作代码示例sizelengthcapcityemptyresizereverse3.4string类对象的访问......
  • C++拷贝构造函数、运算符重载函数、赋值运算符重载函数、前置++和后置++重载等的介绍
    文章目录前言一、拷贝构造函数1.概念2.特征3.编译器生成默认拷贝构造函数4.拷贝构造函数典型使用场景二、运算符重载函数三、赋值运算符重载函数1.赋值运算符重载格式2.赋值运算符只能重载成类的成员函数不能重载成全局函数3.编译器生成一个默认赋值运算符重载四......
  • C++学习笔记----重载运算符
    运算符重载运算符重载可以在通过成员函数或者全局函数进行重载,编译器提供了内置的运算符;我们可以通过定义对应的函数和参数进行重载,也可以使用编译器提供的名称`operator运算符()`进行重载;运算符重载本质是对内置的运算符函数进行重载:函数相同,参数不同;返回引用和地址需要思......
  • 从12个视角看全球C++程序员2/5:深度解读JetBrains最新报告
    讲动人的故事,写懂人的代码4C++程序员最常使用哪种IDE?总体情况(General)VisualStudioCode(VSCode):27%CLion:26%VisualStudio:26%Vi/Vim:4%QtCreator:3%其他工具的使用率较低,分别占据1-2%。嵌入式开发(Embedded)VisualStudioCode:34%CLion:29%VisualStudio:19%......
  • c++ 结构体 联合体 枚举
    结构体:结构体是一种特殊形态的类在C语言中,结构体是一种复合数据类型,用于将多个基本类型或其他复合类型的数据组合成一个整体。结构体中的成员可以是不同的类型,并且可以有不同的访问权限(在C语言中,所有成员默认都是公有的)。在C语言中,结构体不支持成员函数和继承等面向对象的特......
  • c++类的组合
    类的组合描述的是一个类内嵌其他类的对象作为成员的情况,它们之间的关系是一种包含与被包含的关系classEngine{public:voidstart(){//启动引擎的逻辑std::cout<<"Enginestarted!"<<std::endl;}//...其他引擎相关......
  • C++用扩充的结构体类型求复数的绝对值
    结构体的扩充,声明一个扩充的结构体complex,complex结构体中两个双精度数据real和imag,分别代表实数部分和虚数部分 另外还有两个属于结构体的函数:init,asbcomplex,其中init用来给real和imag赋初值,asbcomplex用来计算和返回复数的绝对值real和imag是数据成员函数init和asbcom......
  • 理解C++虚函数和虚表(vtbl)机制
    引言C++是一种强大且灵活的编程语言,它支持面向对象编程(OOP)的各种特性,其中虚函数(virtualfunction)是实现多态性(polymorphism)的关键机制。本文将深入探讨虚函数的原理、虚表(vtbl)的作用,以及这些特性在实际编程中的实现。通过理解这些概念,您将能够更好地掌握C++的多态性和面向......
  • Effective C++ 改善程序与设计的55个具体做法笔记与心得 4
    四.设计与声明18.让接口容易被正确使用,不易被误用请记住:好的接口很容易被正确使用,不容易被误用。你应该在你的所有接口中努力达成这些性质“促进正确使用”的办法包括接口的一致性,以及与内置类型的行为兼容。“阻止误用”的办法包括建立新类型、限制类型上的操作、束缚......