C++ 运行长度编码算法[数据压缩]

C++ 运行长度编码算法[数据压缩],c++,C++,我试图用简单的输入实现算法RLE,如: ddddddddddhhhhhhhhhhhhhhhttttttttttttt 代码: 请帮我查一下密码 更新:我更新了这个问题,因为我意识到了一些事情 另外:这段代码没有输出,我做错了什么吗 char o; char n; int count=0; for(int i=0; i<read.size(); i++) { o = read[i]; n = read[++i]; while(o == n) { co

我试图用简单的输入实现算法RLE,如:

ddddddddddhhhhhhhhhhhhhhhttttttttttttt
代码:

请帮我查一下密码

更新:我更新了这个问题,因为我意识到了一些事情

另外:这段代码没有输出,我做错了什么吗

char o;
char n; 
int count=0; 

for(int i=0; i<read.size(); i++) { 

   o = read[i]; 
   n = read[++i]; 
while(o == n) { 
      count++; 
} 

   cout << o << count; 

      if(o != n) { 
           cout << o << "1"; 
      } count = 0; 
} 
return 0;
charo;
字符n;
整数计数=0;
对于(int i=0;i此循环:

char x;
int count=0;

for(int i=0; i<read.size(); i++) {

    int j=i;
    x = read[i];

    if(x != read[++j]) {
      cout << x << "1";
    }

    while(x == read[++j]) {
        count++;
    }
    cout << x << count;
}

解决此问题的正确方法是a)学习使用调试器的单步功能并观察正在发生的事情,或b)在纸上“运行”代码并查看正在发生的事情。“学会钓鱼”将为您提供一辈子的好代码。char将保存所有ascii值,但不会保存扩展的Unicode字符。它还将保存值-128到127(如果有符号),0到255(如果无符号),这可以用于运行长度编码系统。这个怎么样??查尔o;字符n;整数计数=0;对于(int i=0;i
char o;
char n; 
int count=0; 

for(int i=0; i<read.size(); i++) { 

   o = read[i]; 
   n = read[++i]; 
while(o == n) { 
      count++; 
} 

   cout << o << count; 

      if(o != n) { 
           cout << o << "1"; 
      } count = 0; 
} 
return 0;
char x;
int count=0;

for(int i=0; i<read.size(); i++) {

    int j=i;
    x = read[i];

    if(x != read[++j]) {
      cout << x << "1";
    }

    while(x == read[++j]) {
        count++;
    }
    cout << x << count;
}
// If there are no characters finish
if (read.empty()) {
    return 0;
}
// Get the first character
char lastChar = read[0];
int count = 1;  //  We have counted one character for now
// Go through each character (note start from 1 instead of 0)
for(int i = 1; i < read.size(); i++) {
    // Get the next char
    char newChar = read[i];
    // If it is different, print the current count and reset the counter
    if (lastChar != newChar) {
        cout << lastChar << count;
        count = 1;
        lastChar = newChar;
    } else {  // Else increase the counter
        count++;
    }
}
// Print the last one
cout << lastChar << count;

return 0;