P9459 「EZEC-14」浴眼盯真

记录24

#include <bits/stdc++.h>
using namespace std;
int main(){
  string a,b,c,d;
  int n;
  string y="y",cc="ding",dd="zhen";
  cin>>n;
  for(int i=1;i<=n;i++){
      cin>>a>>b>>c>>d;
  if(a[0]==y[0]&&b[0]==y[0]&&c==cc&&d==dd) cout<<"Yes";
  else cout<<"No";
    cout<<endl;
  }
  return 0;
}

突破口

  • a,b 的首字母均为 y。
  • c 恰好等于 ding。
  • d 恰好等于 zhen。

思路

a,b,c,d四段字符都有其对应的条件,进行对比即可

代码简析

string y="y",cc="ding",dd="zhen";

提前准备好需要进行对比的字符串数据

if(a[0]==y[0]&&b[0]==y[0]&&c==cc&&d==dd)

按照题目的要求来对字符串进行对比

补充

C++中的 std::string 类型

1. 概述

  • 定义std::string 是C++标准库中的一个类,用于表示和操作字符串。

  • 头文件#include <string>

  • 命名空间std::string 位于 std 命名空间中。

2. 基本特性

2.1 动态内存管理
  • std::string 是一个动态数组,可以自动管理内存。当字符串的大小发生变化时,std::string 会自动调整内存分配。

2.2 功能丰富
  • 提供了丰富的成员函数和操作符,用于字符串的创建、修改、查询和操作。

2.3 安全性
  • std::string 提供了边界检查和异常处理机制,避免了C风格字符串常见的安全问题,如缓冲区溢出。

3. 常用成员函数

3.1 构造函数
  • 默认构造:创建一个空字符串。

    std::string s;
  • 从C风格字符串构造:从C风格字符串构造一个 std::string

    const char* cstr = "Hello";
    std::string s(cstr);
  • 从另一个字符串构造:从另一个 std::string 构造一个新字符串。

    std::string s1 = "Hello";
    std::string s2(s1);
3.2 字符访问
  • operator[]:通过下标访问字符串中的字符。

    std::string s = "Hello";
    char c = s[1]; // c = 'e'
  • at():通过下标访问字符串中的字符,并进行边界检查。

    std::string s = "Hello";
    char c = s.at(1); // c = 'e'
3.3 字符串拼接
  • operator+:连接两个字符串。

    std::string s1 = "Hello";
    std::string s2 = "World";
    std::string s3 = s1 + " " + s2; // s3 = "Hello World"
  • operator+=:将一个字符串追加到另一个字符串的末尾。

    std::string s1 = "Hello";
    s1 += " World"; // s1 = "Hello World"
3.4 字符串比较
  • operator==:比较两个字符串是否相等。

    std::string s1 = "Hello";
    std::string s2 = "Hello";
    bool isEqual = (s1 == s2); // isEqual = true
  • operator<:比较两个字符串的字典顺序。

    std::string s1 = "Apple";
    std::string s2 = "Banana";
    bool isLess = (s1 < s2); // isLess = true
3.5 字符串操作
  • size():返回字符串的长度。

    std::string s = "Hello";
    size_t len = s.size(); // len = 5
  • length():返回字符串的长度。

    std::string s = "Hello";
    size_t len = s.length(); // len = 5
  • empty():检查字符串是否为空。

    std::string s = "";
    bool isEmpty = s.empty(); // isEmpty = true
  • clear():清空字符串。

    std::string s = "Hello";
    s.clear(); // s = ""
  • substr():返回字符串的一个子串。

    std::string s = "Hello World";
    std::string sub = s.substr(6, 5); // sub = "World"

4. 常用操作符

4.1 输入输出
  • std::cin:从标准输入读取字符串。

    std::string s;
    std::cin >> s; // 输入 "Hello"
  • std::cout:将字符串输出到标准输出。

     
    std::string s = "Hello";
    std::cout << s << std::endl; // 输出 "Hello"
4.2 拼接
  • operator+:连接两个字符串。

    std::string s1 = "Hello";
    std::string s2 = "World";
    std::string s3 = s1 + " " + s2; // s3 = "Hello World"
4.3 比较
  • operator==:比较两个字符串是否相等。

    std::string s1 = "Hello";
    std::string s2 = "Hello";
    bool isEqual = (s1 == s2); // isEqual = true

5. 示例代码

5.1 字符串拼接和比较

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

int main() {
    string s1 = "Hello";
    string s2 = "World";
    string s3 = s1 + " " + s2; // s3 = "Hello World"
    cout << s3 << endl; // 输出 "Hello World"

    bool isEqual = (s1 == s2); // isEqual = false
    cout << (isEqual ? "Equal" : "Not Equal") << endl; // 输出 "Not Equal"

    return 0;
}
5.2 字符串操作

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

int main() {
    string s = "Hello World";
    cout << "Original string: " << s << endl; // 输出 "Hello World"

    size_t len = s.size(); // len = 11
    cout << "Length: " << len << endl; // 输出 "Length: 11"

    bool isEmpty = s.empty(); // isEmpty = false
    cout << (isEmpty ? "Empty" : "Not Empty") << endl; // 输出 "Not Empty"

    s.clear(); // s = ""
    cout << "After clear: " << s << endl; // 输出 ""

    return 0;
}

6. 总结

std::string 是C++标准库中一个非常强大和灵活的字符串类,提供了丰富的成员函数和操作符,用于字符串的创建、修改、查询和操作。使用 std::string 可以避免C风格字符串常见的安全问题,提高代码的可读性和安全性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值