Files
XCSDD/docs/api/containers/string/operator-subscript.md
ssdfasd 58a83f445a fix: improve doc link navigation and tree display
- Fix link resolution with proper relative/absolute path handling
- Improve link styling with underline decoration
- Hide leaf nodes from tree, only show directories
- Fix log file path for packaged app
2026-03-19 12:44:08 +08:00

1.1 KiB

String::operator[]

char& operator[](SizeType index);
const char& operator[](SizeType index) const;

通过索引访问字符串中的字符。

参数:

  • index - 要访问的字符位置(从 0 开始)

返回: 位置 index 处字符的引用(可写或只读)

复杂度: O(1)

注意: 不进行边界检查。调用者需确保 index < Length()

示例:

#include "XCEngine/Containers/String.h"
#include <iostream>

int main() {
    XCEngine::Containers::String s("Hello");

    // 只读访问
    for (XCEngine::Containers::String::SizeType i = 0; i < s.Length(); ++i) {
        std::cout << s[i];
    }
    std::cout << std::endl;  // 输出: Hello

    // 可写访问
    s[0] = 'J';
    s[1] = 'a';
    s[4] = '!';
    std::cout << s.CStr() << std::endl;  // 输出: Jallo!

    // const 版本
    const XCEngine::Containers::String& cs = s;
    char first = cs[0];
    std::cout << "First char: " << first << std::endl;  // 输出: First char: J

    return 0;
}

相关文档