简体   繁体   English

字符串中单词的相反顺序

[英]Reverse order of words in a string

I am working a problem to reverse the order of words in a string. 我正在解决一个问题,以反转字符串中的单词顺序。 For example, if you have str1 = "the sky is blue" then the solution should be "blue is sky the". 例如,如果您拥有str1 =“ the sky is blue”,则解决方案应为“ blue is sky the”。

Here is my code: 这是我的代码:

class Solution 
{
    public:
        list<string> words;

        void createList(string s)
        {
            istringstream iss(s);
            string token;

            while(getline(iss, token, ' '))
            {
                words.push_back(token);
            }
        }

        string reverseWords(string s) 
        {
            list<string>::iterator iter = words.begin();
            string newString = "";

            createList(s);
            newString.append(*iter);
            for (iter = (iter+1); iter != words.end(); iter++)
            {
                newString.append(" ");
                newString.append(*iter);
            }

            return newString;
        }
};

My question is.... Am I using the list iterator correctly? 我的问题是...。我是否正确使用列表迭代器? I got a compiler error that said "Line 25: no match for 'operator+' " referring to the for loop in reverseWords(). 我收到一个编译器错误,指出“第25行:'operator +'不匹配”,引用了reverseWords()中的for循环。

list does not support random access iterators, therefore using operator+ with list is not allowed, you would use operator++. list不支持随机访问迭代器,因此不允许将operator +与list一起使用,而应使用operator ++。 You have to access the list elements sequentially, just like you'd have to if you were using your own linked list implementation. 您必须顺序访问列表元素,就像使用自己的链表实现时一样。

You could use a reverse iterator, to reverse your string. 您可以使用反向迭代器来反向您的字符串。

#include <list>
#include <string>
#include <iostream>
#include <sstream>

using namespace std;

class Solution 
{
    public:
        list<string> words;

        void createList(string& s)
        {
            istringstream iss(s);
            string token;

            while(getline(iss, token, ' '))
            {
                words.push_back(token);
            }
        }

        string reverseWords(string& s) 
        {
            list<string>::reverse_iterator iter = words.rbegin();
            string newString = "";

            createList(s);
            for ( ; iter != words.rend(); ++iter)
            {
                newString.append(" ");
                newString.append(*iter);
            }

            return newString;
        }
};

int main(int, char**)
{
    string in("The sky is always blue");
    Solution s;
    string out = s.reverseWords(in);

    std::cout << in << std::endl;
    std::cout << out << std::endl;
}

this compiles fine, so yes the ++iter is a valid statement. 这可以很好地编译,因此++ iter是有效的语句。 Incrementing is valid, jumping is not: 增量有效,跳跃无效:

list<string> words;

void createList(string s)
{
    istringstream iss(s);
    string token;

    while(getline(iss, token, ' '))
    {
        words.push_back(token);
    }
}

string reverseWords(string s) 
{
    list<string>::iterator iter = words.begin();
    string newString = "";

    createList(s);
    newString.append(*iter);
    ++iter;
    for (; iter != words.end(); ++iter)
    {
        newString.append(" ");
        newString.append(*iter);
    }

    return newString;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM