标准库标头 <string_view> (C++17)学习

此头文件是字符串库的一部分。本篇介绍string_view的基本用法。

函数

operator==

以字典序比较两个字符串视图
(函数模板)

operator<<

(C++17)

进行字符串视图的流输出
(函数模板)

swap

交换两个对象的值
(函数模板)
范围访问

begincbegin

(C++11)(C++14)

返回指向容器或数组起始的迭代器
(函数模板)

endcend

(C++11)(C++14)

返回指向容器或数组结尾的迭代器
(函数模板)

rbegincrbegin

(C++14)

返回指向一个容器或数组的逆向迭代器
(函数模板)

rendcrend

(C++14)

返回容器或数组的逆向尾迭代器
(函数模板)

sizessize

(C++17)(C++20)

返回容器或数组的大小
(函数模板)

empty

(C++17)

检查容器是否为空
(函数模板)

data

(C++17)

获得指向底层数组的指针
(函数模板)

示例代码:

#include <iomanip>
#include <iostream>
#include <string_view>
#include <cstring>

//https://zh.cppreference.com/w/cpp/header/string_view


#pragma warning(disable:4996)
//C4996	'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.


//C++17标准
int main()
{
    //operator<<  example   (C++17)进行字符串视图的流输出
    constexpr std::string_view s{ "abc" };
    constexpr int width{ 5 };

    // fill/left/right 属性保留不变直至被修改
    std::cout << std::setfill('-');
    std::cout << std::left;

    std::cout << '[' << std::setw(width) << s << "]\n";
    std::cout << '[' << std::setw(width) << s << "]\n";

    std::cout << std::right;
    std::cout << '[' << std::setw(width) << s << "]\n";

    // 每次调用后重置宽度
    std::cout << '[' << s << "]\n";

    //swap example
    std::string_view s1{ "first" };
    std::string_view s2{ "were" };
    std::cout << "s1===========" << s1 << "\n";
    std::cout << "s2===========" << s2 << "\n";
    s2.swap(s1);
    std::cout << "s1===========" << s1 << "\n";
    std::cout << "s2===========" << s2 << "\n";


    //operator== example
    std::cout << "(s1==s2)=============" << (s1 == s2) << "\n";
    
    const char* source = "123456789";
    char dest[20];
    strcpy_s(dest, 10, source);//numberOfElements等于字符串大小
    std::cout << dest << "*\n";

    //data example
    std::string_view s3{"hello world"};
    char a3[32] = {'\0'};
    const char* src = s3.data();
    std::cout << "src==========" << src << "\n";
    std::strcpy(a3, s3.data());
    std::cout << "s3===========" << s3 << "\n";
    std::cout << "a3===========" << a3 << "\n";

    //empty example
    std::cout << "s3.empty=====" << s3.empty() << "\n";
    //size example
    std::cout << "s3.size======" << s3.size() << "\n";
    //begin example
    //std::string_view::iterator it = s3.begin();
    auto begin = s3.begin();
    std::cout << "begin========" << *begin << "\n";
    //cbegin example
    auto cbegin = s3.cbegin();
    std::cout << "cbegin=======" << *cbegin << "\n";

    //end example
    auto end = s3.end();
    end--;
    std::cout << "end==========" << *end << "\n";

    //cend example
    auto cend = s3.cend();
    cend--;
    std::cout << "cend=========" << *cend << "\n";
    //begin,cbegin,end,cend是一样的都不能修改值

    //rbegin example
    auto rbegin = s3.rbegin();
    std::cout << "rbegin========" << *rbegin << "\n";
    //crbegin example
    auto crbegin = s3.crbegin();
    std::cout << "crbegin=======" << *crbegin << "\n";

    //rend example
    auto rend = s3.rend();
    rend--;
    std::cout << "rend==========" << *rend << "\n";

    //crend example
    auto crend = s3.crend();
    crend--;
    std::cout << "crend=========" << *crend << "\n";

}

运行结果:

 参考:

标准库标头 <string_view> (C++17) - cppreference.com

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了将给定的文件汇编成所需的形式,我们需要编写一些规则。这些规则将告诉 Yacc 如何解析输入文件并生成汇编代码。以下是一个可能的解决方案: ``` %{ #include <stdio.h> #include <stdlib.h> #include <string.h> int lineno = 1; %} %token INT ID VOID IF ELSE RETURN WHILE NUMBER %% program: | program external_declaration ; external_declaration: declaration | function_definition ; declaration: INT ID ';' ; function_definition: type_specifier ID '(' parameter_list ')' compound_statement ; type_specifier: INT | VOID ; parameter_list: parameter_declaration | parameter_list ',' parameter_declaration ; parameter_declaration: type_specifier ID | type_specifier ID '[' ']' ; compound_statement: '{' '}' | '{' statement_list '}' ; statement_list: statement | statement_list statement ; statement: expression_statement | compound_statement | selection_statement | iteration_statement ; expression_statement: ';' | expression ';' ; selection_statement: IF '(' expression ')' statement | IF '(' expression ')' statement ELSE statement ; iteration_statement: WHILE '(' expression ')' statement ; expression: assignment_expression | expression ',' assignment_expression ; assignment_expression: equality_expression | unary_expression '=' assignment_expression ; equality_expression: relational_expression | equality_expression '==' relational_expression | equality_expression '!=' relational_expression ; relational_expression: additive_expression | relational_expression '<' additive_expression | relational_expression '>' additive_expression ; additive_expression: multiplicative_expression | additive_expression '+' multiplicative_expression | additive_expression '-' multiplicative_expression ; multiplicative_expression: unary_expression | multiplicative_expression '*' unary_expression | multiplicative_expression '/' unary_expression ; unary_expression: postfix_expression | '-' unary_expression ; postfix_expression: primary_expression | postfix_expression '[' expression ']' | postfix_expression '(' ')' | postfix_expression '(' argument_expression_list ')' ; primary_expression: ID | NUMBER | '(' expression ')' ; argument_expression_list: assignment_expression | argument_expression_list ',' assignment_expression ; %% int main() { yyparse(); return 0; } void yyerror(char *s) { fprintf(stderr, "line %d: %s\n", lineno, s); } int yywrap() { return 1; } ``` 这是一个非常基本的 Yacc 规则集,它定义了语言的各个部分。您需要将此代码保存到名为 `grammar.y` 的文件中。 接下来,我们需要创建一个名为 `lex.l` 的新文件,其中包含与语法规则匹配的词汇表。 ``` %{ #include "y.tab.h" %} %% "int" { return INT; } "void" { return VOID; } "if" { return IF; } "else" { return ELSE; } "return" { return RETURN; } "while" { return WHILE; } [0-9]+ { yylval.num = atoi(yytext); return NUMBER; } [a-zA-Z][a-zA-Z0-9]*{ yylval.str = strdup(yytext); return ID; } "[" { return '['; } "]" { return ']'; } "(" { return '('; } ")" { return ')'; } ";" { return ';'; } "{" { return '{'; } "}" { return '}'; } "+" { return '+'; } "-" { return '-'; } "*" { return '*'; } "/" { return '/'; } "==" { return EQ; } "!=" { return NE; } "<" { return '<'; } ">" { return '>'; } "=" { return '='; } "," { return ','; } [ \t\n] { /* ignore whitespace */ } . { printf("Unknown token: %s\n", yytext); } %% int main() { yyparse(); return 0; } void yyerror(char *s) { fprintf(stderr, "line %d: %s\n", yylineno, s); } int yywrap() { return 1; } ``` 请注意,此文件包含与 `y.tab.h` 匹配的标头文件,并且包含了一些正则表达式以匹配输入文件中的单词。您需要将此文件保存为 `lex.l`。同时,还需要生成一个头文件 `y.tab.h`,这可以通过在终端中执行以下命令来完成: ``` yacc -d grammar.y ``` 最后,我们需要生成一个可执行文件并测试它是否按预期工作。这可以通过在终端中执行以下命令来完成: ``` yacc -d grammar.y lex lex.l cc y.tab.c lex.yy.c -o compiler ./compiler < input.txt ``` 其中,`input.txt` 是您想要编译的输入文件。如果一切正常,您将会看到输出文件的内容显示在终端上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值