小编sau*_*abh的帖子

为什么字符串的行为在这里不同?为什么第一个for循环打印没有,第二个正确打印输出

在这种String长度编码算法中,我无法理解字符串的奇怪行为.任何人都可以解释字符串的用法吗?for loop下面的第一个打印没有,而第二个正确打印.我尝试使用大小,长度和调整大小,仍然for loop没有打印任何东西.

#include <iostream>
using namespace std;

int main() {
    // your code goes here
    string str, newstr;
    str="aabcccccaaa";
    newstr[0]=str[0];
    cout<<str;
    int j = 1;
    for (int i =0; i<str.length(); i++)
    {
            int count =1;
            while(i+1 != str.length() && str[i] == str[i+1])
            {
                count++; i++;
            }
            newstr[j] = '0'+count;
            if(i+1 != str.length())
            {   newstr[j+1] = str[i+1];
                j=j+1;
            }
            j=j+1;
    }
    cout<<"\nRun String:";
    for(int i =0; i<newstr.size(); i++) // First For loop.
        cout<<newstr[i];
    for(int …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

0
推荐指数
1
解决办法
53
查看次数

标签 统计

c++ ×1

c++11 ×1