C++ C++;使用for循环反向打印字符串

C++ C++;使用for循环反向打印字符串,c++,string,for-loop,reverse,C++,String,For Loop,Reverse,我有一个程序,它使用for循环打印出字符串的字符。它还必须反向打印相同的字符,这就是我遇到的问题。有人能帮我弄清楚为什么第二个for循环没有执行吗 int main() { string myAnimal; cout << "Please enter the name of your favorite animal.\n"; cin >> myAnimal; // This loop works fine int i;

我有一个程序,它使用for循环打印出字符串的字符。它还必须反向打印相同的字符,这就是我遇到的问题。有人能帮我弄清楚为什么第二个for循环没有执行吗

int main()
{
    string myAnimal;

    cout << "Please enter the name of your favorite animal.\n";
    cin >> myAnimal;

    // This loop works fine
    int i;
    for(i = 0; i < myAnimal.length(); i++){
        cout << myAnimal.at(i) << endl;
    }

    // This one isn't executing
    for(i = myAnimal.length(); i > -1; i--){
        cout << myAnimal.at(i) << endl;
    }
    return 0;
}
intmain()
{
串珠鼠;
库特>我的动物;
//这个循环很好用
int i;
对于(i=0;i
你是说
i=myAnimal.length()-1

这没有任何作用。您获取值,然后将其丢弃


您的意思是
i=myAnimal.length()-1

首先需要将i指定给长度减1,或数组中的最后一个索引值

for(i = myAnimal.length()-1; i >= 0; i--){
    cout << myAnimal.at(i) << endl;
}
for(i=myAnimal.length()-1;i>=0;i--){

cout首先需要将i赋给长度减1,或数组中的最后一个索引值

for(i = myAnimal.length()-1; i >= 0; i--){
    cout << myAnimal.at(i) << endl;
}
for(i=myAnimal.length()-1;i>=0;i--){

cout因为字符位置从0开始,所以
myAnimal
的最后一个字符在位置
(myAnimal.length()-1)
不是
myAnimal.length()
,所以您想在那里开始第二个循环。

因为字符位置从0开始,所以
myAnimal
的最后一个字符在位置
(myAnimal.length()-1)
而不是
myAnimal.length()
因此您希望在那里开始第二个循环。

而不是

int i;
for(i = 0; i < myAnimal.length(); i++){
    cout << myAnimal.at(i) << endl;
}

// This one isn't executing
for(i = myAnimal.length(); i > -1; i--){
    cout << myAnimal.at(i) << endl;
}
inti;
对于(i=0;i
int i;
for(i = 0; i < myAnimal.length(); i++){
    cout << myAnimal.at(i) << endl;
}

// This one isn't executing
for(i = myAnimal.length(); i > -1; i--){
    cout << myAnimal.at(i) << endl;
}
inti;
对于(i=0;icout@Lokno已经为您提供了正确的答案。不过,让我进一步挑剔一下您的代码,以向您展示其他一些替代方案,并纠正一些小错误

首先,您实际上没有发布编译示例,因为您忘记显示包含的标题
,也没有显示代码中隐含的
使用名称空间std;

其次,对于常规的
for
循环,最好将循环变量保留在循环中,除非您实际需要将其用作返回值。此外,对于增量后
i++
,也要使用
++i
。此外,由于您已经确保了正确的循环索引,因此没有理由使用边界检查的元素访问
>在未选中的
[]
版本上设置at()

在C++11中,循环的范围允许使用更短、更简单的代码,在这里我还使用了
auto
,您可以使用
char
。不幸的是,循环没有反向范围。如果使用
I>=0
而不是
I>的话,正确的基于索引的循环反向可能更容易阅读-1

然后是一个基于算法的循环,使用
std::copy
,使用
std::string
的迭代器接口(特别是反向迭代器
rbegin()
rend()
)通过绑定到标准输出的
ostream\u迭代器
复制每个字符

顺便说一句,我使用了分隔符
“|”
而不是换行符来更容易地查看内容,以适应您的口味。在任何情况下,使用
std::endl
都会有,因为它每次都会刷新输出缓冲区

#include <algorithm>
#include <iterator>
#include <iostream> // you forgot this
#include <string>   // you forgot this

int main()
{
    using namespace std;    // you forgot this

    // let's pretend this is the string
    string myAnimal = "Please enter the name of your favorite animal.";

    // keep the loop variable local, prefer pre-increment
    for (int i = 0; i < myAnimal.length(); ++i)
        cout << myAnimal[i] << "|";    // prefer [] over at()
    std::cout << "\n";

    // C++11 range-for
    for (auto c : myAnimal)
        std::cout << c << "|";
    std::cout << "\n";

    // index-based reverse loop
    for (int i = myAnimal.length() - 1; i >= 0; --i)
        cout << myAnimal[i] << "|";
    std::cout << "\n";

    // algorithm-based reverse loop
    std::copy(myAnimal.rbegin(), myAnimal.rend(), ostream_iterator<char>(cout, "|"));
    std::cout << "\n";

    // main implicitly return 0
}

#include.PS:
main()
在成功后隐式返回
0

@Lokno已经为您提供了正确的答案。不过,让我对您的代码进行更多的挑剔,以向您展示一些其他选择,并纠正一些小错误

首先,您实际上没有发布编译示例,因为您忘记显示包含的标题
,也没有显示代码中隐含的
使用名称空间std;

其次,对于常规的
for
循环,最好将循环变量保留在循环中,除非您实际需要将其用作返回值。此外,对于增量后
i++
,也要使用
++i
。此外,由于您已经确保了正确的循环索引,因此没有理由使用边界检查的元素访问
>在未选中的
[]
版本上设置at()

在C++11中,循环的范围允许使用更短、更简单的代码,在这里我还使用了
auto
,您可以使用
char
。不幸的是,循环没有反向范围。如果使用
I>=0
而不是
I>的话,正确的基于索引的循环反向可能更容易阅读-1

然后是一个基于算法的循环,使用
std::copy
,使用
std::string
的迭代器接口(特别是反向迭代器
rbegin()
rend()
)通过绑定到标准输出的
ostream\u迭代器
复制每个字符

顺便说一句,我使用了分隔符
“|”
而不是换行符来更容易地查看内容,以适应您的口味。在任何情况下,使用
std::endl
都会有,因为它每次都会刷新输出缓冲区

#include <algorithm>
#include <iterator>
#include <iostream> // you forgot this
#include <string>   // you forgot this

int main()
{
    using namespace std;    // you forgot this

    // let's pretend this is the string
    string myAnimal = "Please enter the name of your favorite animal.";

    // keep the loop variable local, prefer pre-increment
    for (int i = 0; i < myAnimal.length(); ++i)
        cout << myAnimal[i] << "|";    // prefer [] over at()
    std::cout << "\n";

    // C++11 range-for
    for (auto c : myAnimal)
        std::cout << c << "|";
    std::cout << "\n";

    // index-based reverse loop
    for (int i = myAnimal.length() - 1; i >= 0; --i)
        cout << myAnimal[i] << "|";
    std::cout << "\n";

    // algorithm-based reverse loop
    std::copy(myAnimal.rbegin(), myAnimal.rend(), ostream_iterator<char>(cout, "|"));
    std::cout << "\n";

    // main implicitly return 0
}
#include.PS:
main()
成功后隐式返回
0

您可以使用

#包括
#包括
int main(){
std::字符串myAnimal;
std::cout>myAnimal;
//按相反顺序迭代
对于(auto c=myAnimal.rbegin();c!=myAnimal.rend();++c){
std::cout您可以使用

#包括
#包括
int main(){
std::字符串myAnimal;
std::cout>myAnimal;
//按相反顺序迭代
对于(auto c=myAnimal.rbegin();c!=myAnimal.rend();++c){
std::您是否至少应该尝试解释更改有帮助的原因。此外,将循环增量移动到
cout
操作中是聪明的,但通常被认为是糟糕的样式。您至少应该尝试解释更改有帮助的原因。此外,将循环增量移动到