English 中文(简体)
通过 C++ 中的迭代更新字符串
原标题:Updating a string through iteration in C++
  • 时间:2012-05-25 01:06:54
  •  标签:
  • c++
  • windows

OK,所以我试着做一个字符串的事情, 这样字符串就可以更新。 有点像你有一个字符串 Hello, 我想要它自己更新一些像 Hh"h"hel"hel"hell"hell"hello

因此,我有:

#include <iostream>
#include <string>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

using namespace std;

int main()
{
    system("title game");
    system("color 0a");
    string sentence = "super string ";

    for(int i=1; i<sentence.size(); i++){
        cout << sentence.substr(0, i) <<endl;
    }
    return 0;
}

代码返回像:

"s "su" "sup" "supe" "super"

显然在不同的线条上, 但当我删除结尾线时, 句子构建者就发狂了。 它展示了类似“ spupppuepr sttrrrrtsubstringsubstrusing substrusing ” 的东西 。

我能否更新同一线上的线条? (而且没有完全销毁)

最佳回答

您可以在每次迭代时打印一个传回字符 < code> , 将光标返回到线条开头 :

for(int i=1; i<sentence.size(); i++){
    cout <<  
  << sentence.substr(0, i);
}

或按顺序输出每个字符 :

for(int i=0; i<sentence.size(); i++){
    cout << sentence[i];
}

您可能还想要插入每次循环迭代的短时间, 以便实现打字机效果 。

问题回答

运行您的代码 产生这个:

./a.out
ssusupsupesupersuper super ssuper stsuper strsuper strisuper strinsuper string

这正是你要求它做的。它与内端相同,但没有新线条。如果您不想它重复所有字母,您需要通过字符串本身而不是通过子字符串循环。

using namespace std;

int main()
{
    system("title game");
    system("color 0a");
    string sentence = "super string ";

    for(int i=0; i<sentence.size(); i++){
        cout << sentence[i];
    }
    return 0;
}

我的建议:使用 While 循环

#include <stdio.h>
#include <iostream>

int main() {
    system("title game");
    system("color 0a");
    char* sentence = "super string";

    while( *sentence ) std::cout <<  *sentence++;
    return 0;
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签