You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++报错:Expression: cannot seek string iterator after end 原因解析

字符串旋转程序报错"Expression: cannot seek string iterator after end"的排查

考试期间编写了一个字符串旋转程序,原本预期可正常运行,但测试时反复出现"Expression: cannot seek string iterator after end"错误,尝试多种方法均无法解决,恳请帮忙分析问题原因。

程序代码

#include <iostream>
#include <cmath>
#include <cctype>
#include <string>

using namespace std;

void rotate_string(string& st, int k, bool right);

int main() {

cout << "Welcome to string rotation!";
string name;
string id;
string name_id;
string phone;

cout << "Enter first name: " << endl;
cin >> name;
cout << "Enter student id: " << endl;
cin >> id;
cout << "Enter phone number (in any format): " << endl;
cin >> phone;
name_id = name + "_" + id;

cout << "name_id before rotation = " << name_id << endl;

cout << "Rotate right for even digit and left for odd digit: " << endl;
for (int i = 0; i < phone.size(); ++i) {
    char num = phone[i];
    if (isdigit(num)) {
        int dig = static_cast<int>(num);
        if ((dig % 2) == 0) {
            rotate_string(name_id, dig, true);
        }
        else {
            rotate_string(name_id, dig, false);
        }
        cout << "digit = " << dig << ", name_id = " << name_id << endl;
        cout << endl;
    }
}
cout << endl;

cout << "Rotated name_id = " << name_id << endl;

cout << "Rotate left for even digit and right for odd digit: " << endl;
for (int i = 0; i < phone.size(); ++i) {
    char num = phone[i];
    if (isdigit(num)) {
        int dig = static_cast<int>(num);
        if ((dig % 2) == 0) {
            rotate_string(name_id, dig, false);
        }
        else {
            rotate_string(name_id, dig, true);
        }
        cout << "digit = " << dig << ", name_id = " << name_id << endl;
        cout << endl;
    }
}
cout << endl;

return 0;

}

void rotate_string(string& st, int k, bool right) {

int n = st.size();
if (right == true) {
    int b = st.size() - k;
    reverse(st.begin(), st.begin() + b);
    reverse(st.begin() + b, st.end());
    reverse(st.begin(), st.end());
}
else {
    reverse(st.begin(), st.begin() + k);
    reverse(st.begin() + k, st.end());
    reverse(st.begin(), st.end());
}

}

已尝试的解决方法

  • 将rotate_string函数放在单独文件中
  • 修改函数名称,去掉其中的string字样
  • 将所有代码复制到新项目中(此方法之前有效)
  • 在另一个项目中单独测试该函数,运行无异常

错误原因分析

核心问题出在字符转数字的逻辑错误:
使用static_cast<int>(num)将字符型数字转换为整数时,得到的是该字符的ASCII码值(例如字符'0'的ASCII码是48,'1'是49,以此类推),而非实际的数字值0-9。

这会引发两个致命问题:

  1. 旋转步数k远大于字符串长度(比如字符'5'会被转成53,而name_id的长度通常远小于这个数值)
  2. 迭代器越界:
    • 左旋转时,st.begin() + k会超出字符串末尾的st.end()迭代器
    • 右旋转时,st.size() - k会得到负数,st.begin() + b会变成小于st.begin()的非法迭代器

两种情况都会触发断言错误"cannot seek string iterator after end"。

修复方案

  1. 修正字符转数字逻辑:
    将字符转数字的代码改为:

    int dig = num - '0';
    

    利用ASCII码的连续性,直接得到字符对应的整数0-9。

  2. 优化旋转函数的鲁棒性:
    在rotate_string函数中对旋转步数取模,并处理空字符串和步数为0的情况,避免无效操作:

    void rotate_string(string& st, int k, bool right) {
        int n = st.size();
        if (n == 0) return; // 空字符串直接返回
        k = k % n; // 取模减少无效旋转次数
        if (k == 0) return; // 步数为0无需旋转
        if (right) {
            int b = n - k;
            reverse(st.begin(), st.begin() + b);
            reverse(st.begin() + b, st.end());
            reverse(st.begin(), st.end());
        } else {
            reverse(st.begin(), st.begin() + k);
            reverse(st.begin() + k, st.end());
            reverse(st.begin(), st.end());
        }
    }
    

内容的提问来源于stack exchange,提问作者EmptyStone

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 23:20:01