【问题标题】:I'm stuck with problem related to string in C++我遇到了与 C++ 中的字符串相关的问题
【发布时间】:2022-01-05 04:08:07
【问题描述】:

我想以相反的顺序显示一个句子。例如“我有钱”到“我有钱”。我使用过这样的堆栈,但我不明白为什么我不能将元素推入堆栈(最后检查时堆栈大小始终 = 0)

#include<bits/stdc++.h>
using namespace std;

int main() {
    string s;
    cin >> s;
    string temp;
    stack<string> stk;
    for (int i = 0; i < s.size(); i++) {
        if (s[i] != ' ') {
           temp.push_back(s[i]);
           cout << temp << endl;
        } else {
            stk.push(temp);
            temp = "";
        }
    }
    for (int i = 0; i < stk.size(); i++) {
        cout << stk.top() << endl;
        stk.pop();
    }
    cout << stk.size();
}

【问题讨论】:

  • 这里有几件事需要解决,但首先是:当你执行“stk.pop()”时,你会自动从堆栈中删除顶部元素。代码末尾的 for 循环将遍历堆栈,打印出所有元素并清空堆栈。这就是为什么在循环之后大小始终为 0。
  • 可以删除第一个for 循环,简单地说:std::istringstream strm(s); while (strm &gt;&gt; temp) stk.push_back(temp); 无需检查空格或其他逻辑,例如将temp 设置为空字符串。
  • @Ballantines 不,输入有问题。 std::cin &gt;&gt; s; 将在第一个空白处停止读取。最后一个循环也有问题。每次迭代都会增加 i,但也会减少 stk.size(),所以你只处理一半的元素。
  • 您应该使用std::getline 读取字符串,而不是cin。你应该打印出你读入的值,你会看到它在第一个空格之后不包含任何单词。

标签: c++ string


【解决方案1】:

这是一个完整的答案。它非常简短。

#include <stack>
#include <string>
#include <iostream>

int main() 
{
    std::string temp;
    std::stack<std::string> stk;
    while (std::cin >> temp)
        stk.push(temp);

    while (!stk.empty())
    {
      std::cout << stk.top() << " ";
      stk.pop();
    }
}  

请注意,没有用于检查空格的循环。

其次,while 循环比你的 for 循环更直观,更容易编写,后者是错误的。在您的for 循环中,您同时有一个缩小的堆栈和一个增加的i 索引,这是错误的。

在清空堆栈时打印出堆栈条目的正确方法是使用while (not empty) 范例,同时在循环内弹出顶部元素。

最后,同样重要的是,代码没有所有缺陷,例如包括 bits 标头和 using namespace std;

Live Example

【讨论】:

    【解决方案2】:

    作为一种变体,您可以考虑解决您的问题。

    #include <iostream>
    #include <vector>
    #include <string>
    using namespace std;
    const int maximumSize=10;
    vector<int> visited(maximumSize, 0);
    vector<string> sentence={"I", "have", "money"};
    vector<string> reverseSentence;
    void showContentVector(vector<string> input)
    {
        for(int i=0; i<input.size(); ++i)
        {
            cout<<input[i]<<" ";
        }
        return;
    }
    void dfs(int current, int previous)
    {
        if(visited[current]==1)
        {
            return;
        }
        visited[current]=1;
        for(int next=(current+1); next<sentence.size(); ++next)
        {
            if(next==previous)
            {
                continue;
            }
            dfs(next, current);
        }
        reverseSentence.push_back(sentence[current]);
        if(current==0)
        {
            showContentVector(reverseSentence);
        }
        return;
    }
    int main()
    {
        dfs(0, -1);
        return 0;
    }
    

    输入:

    I have money
    

    结果如下:

    money have I
    

    【讨论】:

    • 这太过分了。您无需进行深度优先搜索即可完成如此简单的任务。
    • @PaulMcKenzie,尊敬的 McKenzie 先生,您是对的,毫无疑问。此方法被视为解决方案的一种变体,并且有效。
    • 由于我不再认为这是现实,所以用 DFS 答案回答任何问题(请查看用户“Vadim Chernetsov”),我猜这是 stackoverflow 的测试,如何我们很友好,甚至看着,比如说,不恰当的答案。我会一次标记它以检查。 . .
    【解决方案3】:
    #include <bits/stdc++.h>
    using namespace std;
    
    vector <string> splitstringarray;
    void splitstring(string s)
    {
        stringstream ss(s);
        string word;
        while (ss >> word) {
            splitstringarray.push_back(word);
            cout << word << endl;
        }
    }
    
    int main()
    {
        splitstring("you sample string")
        for (int i = splitstringarray.size(); i > 0; i--)
        {
            cout << splitstringarray[i];
        }
        return 0;
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    • 2023-03-30
    • 2013-01-15
    • 1970-01-01
    相关资源
    最近更新 更多