c++ - 我需要更多空间吗?

标签 c++ xcode

我有代码应该将一个字符串分成 3 个长度部分:

ABCDEFG should be ABC DEF G

但是,我有一个非常长的字符串,而且我不断得到

terminate called without an active exception

当我缩短字符串的长度时,它似乎起作用了。我需要更多空间吗?我认为在使用字符串时我不必担心空间问题。

int main ()
{
    string code, default_Code, start_C;
    default_Code = "TCAATGTAACGCGCTACCCGGAGCTCTGGGCCCAAATTTCATCCACT";         
    start_C      = "AUG";

    code         = default_Code;

    for (double j = 0; j < code.length(); j++) {  //insert spacing here
        code.insert(j += 3, 1, ' ');
    }
    cout << code;

    return 0;
}

最佳答案

考虑一下 code.length() == 2 的情况。您在字符串的某处插入了一个空格。我不确定,但如果 for(int j=0; j+3 < code.length(); j++) 就可以了.

关于c++ - 我需要更多空间吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8132437/

相关文章:

C++ 编辑文本文件?

c++ - reinterpret_cast 创建一个简单的默认构造对象

ios - 禁用约束 Storyboard

iphone - 使用自定义字体

c++ - GCC 内联汇编错误 : Cannot take the address of 'this' , 这是一个右值表达式

C++ SFML 2.1 模板错误

c++ - 如何在类中使用 termios.h 注册事件

c++ - 动态加载 DLL 的安全性

ios - 无法呈现和更新自动布局状态 Stripe.Framework

Xcode 7/Swift 2.0、imageWithRenderingMode(.AlwaysTemplate) 和 EXC_BAD_ACCESS