如何删除此代码输出的结束空间

时间:2016-04-23 02:57:04

标签: c++

输出以句子结尾,然后是我需要删除的最后一个空格。请帮忙!我很难过!我知道它最终会在cout中,但是如果我删除了最后一个空格,那么整个输出句子都没有空格。这是我第一次做C ++,所以我在这方面是一个初学者,并且根本不知道现在该做什么。

#include<iostream>
#include<string>
#include<sstream>
#include<algorithm>
#include<iterator>
#include<map>

using namespace std;

map<string, string> myMap;

int contains(string str) {
     map<string,string>::iterator it = myMap.find(str);
     if(it != myMap.end())
     {
     return 1;
     }
     return 0;
     }

 int main() {

     myMap["BFF"] = "best friend forever";
     myMap["IDK"] = "I don't know";
     myMap["JK"] = "just kidding";
     myMap["TMI"] = "too much information";
     myMap["TTYL"] = "talk to you later";
     myMap["IDK."] = "I don't know.";
     myMap["TTYL."] = "talk to you later.";
     myMap["BFF,"] = "best friend forever,";
     myMap["JK,"] = "just kidding,";
     myMap["JK."] = "just kidding.";
     myMap["TMI!"] = "too much information!";
     myMap["TMI."] = "too much information.";


    string text;

    cout << "Enter text: ";
    getline(cin, text);
    cout << "You entered: " << text << endl;
    cout << "Expanded: ";

 stringstream ss(text);
 string item;
 while (getline(ss, item, ' ')) {
     if(contains(item) == 1) {
         cout << myMap[item] << " ";
    } 
    else {
         cout << item << " ";
     }

}


cout << endl;


}

3 个答案:

答案 0 :(得分:1)

我通过在项目之前打印空间来解决这个问题。例如:

cout << " " << item;

问题是,在扩展后,您现在会有一个额外的空间:&#34;。您可以通过添加&#39; if&#39;轻松解决此问题。语句,在你启动while循环之前打印没有空格的第一个东西:

      if(getline(ss, item, ' ')) {
        if(contains(item) == 1) {
          cout << myMap[item];
        } 
        else {
          cout << item;
        } 
      }
      while(getline....)

或者,您可以删除打印位置的尾随空格&#34; Expanded:&#34; :)

答案 1 :(得分:1)

另一种方法是使用std::string作为临时缓冲区,并在打印之前删除最后一个字符(额外空格)。

您也可以避免将地图声明为全局变量,并将其作为参数传递给contains函数。这是一个设计选择,我不知道你的实际代码有多少取决于myMap是全局的。

这就是你可以改变代码的方式:

#include<iostream>
#include<string>
#include<sstream>
#include<map>

using std::cout;
using std::endl;
using std::string;
using std::map;

bool contains( map<string, string> m, string str) {
    return  m.find(str) != m.end();
}

int main() {
    map<string, string> myMap{
        {"BFF", "best friend forever"},
        {"IDK", "I don't know"},
        {"JK", "just kidding"},
        {"TMI", "too much information"},
        {"TTYL", "talk to you later"},
        {"IDK.", "I don't know."},
        {"TTYL.", "talk to you later."},
        {"BFF,", "best friend forever,"},
        {"JK,", "just kidding,"},
        {"JK.", "just kidding."},
        {"TMI!", "too much information!"},
        {"TMI.", "too much information."},
    };

    string text;

    cout << "Enter text: ";
    getline(std::cin, text);
    cout << "You entered: " << text << endl;
    cout << "Expanded: ";

    std::stringstream ss(text);
    string item;
    string outs;
    while ( ss >> item ) {             // spaces are delimiters for operator>>
        if( contains(myMap, item) ) {
            outs += myMap[item];
        } 
        else {
            outs += item;
        }
        // or, if you know the conditional operator:
        //                outs += contains(myMap, item) ? myMap[item] : item;

        outs += ' ';
    }
    outs.pop_back();                   // removes the last char
    cout << outs << endl;

    return 0;                          
}

请注意,您要在地图中搜索两次项目,可以通过这样做来避免:

while ( ss >> item ) {
    map<string, string>::iterator it = myMap.find(item);
    outs += (it != myMap.end() ) ? it->second : item;
    ...

答案 2 :(得分:1)

只需将其作为前缀,而不是后缀,空格:

cout << "Expanded:";               // Removed trailing space here

stringstream ss(text);
string item;
while (getline(ss, item, ' ')) {
    cout << ' ';                   // now adding space FIRST
    if (contains(item) == 1)
       cout << myMap[item];
    else
       cout << item;
}

现在,该行的末尾没有空格(无论您是否有物品),但每个项目(如果您有一些)仍然用空格分隔。