qt-C++笔记之contains和isEmpty函数、以及部分其他函数列举

时间:2025-03-17 10:50:02

qt-C++笔记之contains()和isEmpty()函数、以及部分其他函数列举

code review!

参考博文
qt-C++笔记之contains()和isEmpty()函数、以及部分其他函数列举

文章目录

  • qt-C++笔记之contains()和isEmpty()函数、以及部分其他函数列举
      • contains()
      • isEmpty()
    • 类似的其他函数列举
      • 通用容器类函数
      • 字符串特有函数

在Qt C++开发中, contains()isEmpty() 是两个常用的函数,它们通常用于不同的类和上下文中来检查容器的内容。这里简要解释一下这两个函数的用法:

contains()

contains() 函数通常用于检查容器(如QString, QList, QMap等)是否包含某个特定的元素或键值对。根据所使用的容器类型,它的确切功能可能略有不同,但基本的用法是相似的。

  • QString: 检查字符串是否包含一个子串。

    QString str = "Hello, world!";
    bool containsHello = str.contains("Hello"); // 返回 true
    
  • QList: 检查列表中是否存在某个元素。

    QList<int> list = {1, 2, 3};
    bool containsTwo = list.contains(2); // 返回 true
    
  • QMap: 检查映射中是否存在某个键。

    QMap<QString, int> map;
    map["apple"] = 2;
    map["banana"] = 3;
    bool containsApple = map.contains("apple"); // 返回 true
    

isEmpty()

isEmpty() 函数用于检查容器是否为空。在不同的容器类中,它用来判断容器内是否没有任何元素。

  • QString: 检查字符串是否为空。

    QString str;
    bool empty = str.isEmpty(); // 如果str是空的,返回 true
    
  • QList: 检查列表是否没有任何元素。

    QList<int> list;
    bool empty = list.isEmpty(); // 如果list是空的,返回 true
    
  • QMap: 检查映射是否没有任何键值对。

    QMap<QString, int> map;
    bool empty = map.isEmpty(); // 如果map是空的,返回 true
    

这些函数都是非常高效的,因为它们通常是在容器的内部实现中直接检查状态,而不需要遍历整个容器。

使用这些函数时,请确保你的容器已经被适当地初始化,否则可能会遇到未定义的行为。在Qt中使用这些函数可以帮助你编写更加清晰和高效的代码。

类似的其他函数列举

Qt框架中提供了大量的函数来操作和查询其内置的数据结构。除了contains()isEmpty()之外,还有许多其他有用的成员函数。下面列举了一些常见的函数,这些函数通常可用于各种容器类,如QString、QList、QVector、QMap、QSet等。

通用容器类函数

  • size() / count(): 返回容器中的元素数量。

    QList<int> list = {1, 2, 3};
    int size = list.size(); // 返回 3
    
  • at() / operator[]: 返回容器中特定位置的元素(at()通常是只读的,而operator[]可以用于修改)。

    QVector<int> vector = {1, 2, 3};
    int value = vector.at(1); // 返回 2
    
  • front() / back(): 返回容器中的第一个/最后一个元素。

    QList<int> list = {1, 2, 3};
    int front = list.front(); // 返回 1
    int back = list.back(); // 返回 3
    
  • begin() / end(): 提供迭代器到容器的开始和结束。

    QList<int> list = {1, 2, 3};
    auto it = list.begin();
    while (it != list.end()) {
        // Do something with *it
        ++it;
    }
    
  • insert(): 在容器中插入元素。

    QList<int> list;
    list.insert(list.begin(), 42); // 在list的开始位置插入 42
    
  • remove(): 移除容器中的特定元素。

    QVector<int> vector = {1, 2, 3};
    vector.remove(1); // 移除索引为 1 的元素(即移除 2)
    
  • clear(): 清空容器中的所有元素。

    QMap<QString, int> map;
    map["apple"] = 2;
    map.clear(); // 清空map
    

字符串特有函数

  • startsWith() / endsWith(): 检查字符串是否以特定的子串开始/结束。

    QString str = "Hello, world!";
    bool starts = str.startsWith("Hello"); // 返回 true
    bool ends = str.endsWith("world!"); // 返回 true
    
  • split(): 将字符串按照指定的分隔符分割为子串列表。

    QString str = "apple,banana,cherry";
    QStringList fruits = str.split(","); // 返回 {"apple", "banana", "cherry"}
    
  • toInt() / toFloat() / toDouble(): 转换字符串为整数/浮点数/双精度浮点数。

    QString number = "42";
    int value = number.toInt(); // 返回 42
    
  • trimmed(): 返回去除字符串两端空白字符的副本。

    QString str = "  Hello, world!  ";
    QString trimmedStr = str.trimmed(); // 返回 "Hello, world!"
    
  • toUpper() / toLower(): 将字符串转换为大写/小写。

    QString str = "Hello, World!";
    QString upperStr = str.toUpper(); // 返回 "HELLO, WORLD!"
    QString lowerStr = str.toLower(); // 返回 "hello, world!"