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

时间:2014-01-26 23:49:08

标签: 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(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;
}

6 个答案:

答案 0 :(得分:5)

您需要首先将i分配给长度减去1或数组中的最后一个索引值。

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

@Lokno已经为您提供了正确的答案。但是,让我更多地挑选您的代码,向您展示其他一些替代方案并纠正一些小错误。

首先,您实际上没有发布编译示例,因为您忘记显示包含的标题<iostream><string>,也没有显示隐含的using namespace std;你的代码。

其次,对于常规for循环,更喜欢将循环变量保留在循环中,除非您确实需要将其用作返回值。同样prefer pre-increment ++i超过后增量i++。此外,由于您已确定正确的循环索引,因此没有理由对未经检查的at()版本使用bounds-checked元素访问[]

在C ++ 11中,你有range-for循环,它允许更短,更简单的代码,我也使用auto你可以使用char。不幸的是,没有反向范围 - for循环。如果使用i >= 0而不是i > -1,则可能更容易阅读正确的基于索引的反向循环。

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

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

#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
}

Live Example。 PS:main()在成功时隐式返回0

答案 3 :(得分:0)

您可以使用reverse iterators

me@meanwhileinhell:~$ ls -al
-rw-r--r--  1 me me  1672 Sep 28 11:02 createTables.cql

me@meanwhileinhell:~$ docker exec -i cassandra_1 cqlsh -f createTables.cql
Can't open 'createTables.cql': [Errno 2] No such file or directory: ‘createTables.cql'

请注意,您必须增加变量&#39; c&#39; (而不是递减)因为这是一个反向迭代器。

答案 4 :(得分:-1)

for(myAnimal.length(); i > -1; i--){
    ^

这没有做任何事情。你获取值然后扔掉它。

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

答案 5 :(得分:-3)

而不是

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;
}

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

// This one isn't executing
for ( string::size_type i = myAnimal.length(); i != 0; ){
    cout << myAnimal.at( --i) << endl;
}

在您的代码中,您尝试访问超出可接受范围的字符串元素,该范围等于[0,length() - 1]

而不是类型int,最好使用std :: string为成员函数长度的返回类型提供的类型,即std :: string :: size_type。