在C ++中删除多余的空格 [英] Removing Extra Whitespace in C++

查看:379
本文介绍了在C ++中删除多余的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写从文件或标准输入中读取的代码,并导致文件或标准输入中的所有空格都被压缩",这意味着序列中只有最后一个空格字符空白打印.我的方法是创建一个地图,其中每个单词都作为键,而每个单词后的空格都作为其值.这可能是有问题的,因为我需要按插入顺序打印地图的内容,而且我还会有重复的键.我发现了关于std :: unordered_multimap的信息,但是我很难弄清楚如何实现它.

I'm trying to write code that reads in from a file, or standard input, and causes all whitespace in the file, or standard input, to be "squished", meaning that only the last whitespace character in a sequence of whitespace is printed. My approach was to create a map with every word as a key and the whitespace after that word as it's value. This might be problematic as I would need the map's contents to be printed in order of insertion and I would also have duplicate keys. I found out about std::unordered_multimap but I am having trouble figuring out how I would go about implementing it.

这就是我所拥有的:

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iterator>
#include <algorithm>
#include <sstream>
#include <map>

using namespace std;

int main(int argc, char** argv) {

    string filename;
    char input;
    map<string, string> words;
    ostringstream os;
    string word = "";
    string spaces = "";

    filename = argv[argc-1];
    ifstream infile(filename);

    while ( infile.get(input) ) {
        os << input;
    }

    string filecontents = os.str();

    for (int i = 0; i < filecontents.length(); ++i) {
        if ( !isspace(filecontents[i])) {
            if (spaces.length() >= 1) {
                words[word] = spaces;
                word = "";
                spaces = "";
                word += filecontents[i];
            }

            else {
                word += filecontents[i];
            }
        }

        else {
            spaces += filecontents[i];
        }
    }

    words[word] = "";

    for (const auto& p : words) {
        cout << p.first << p.second.back();
    }

文件:

potato   milk   

sausage

输出:

milk
potato sausage

也许有更好的方法可以做到这一点?我应该补充一点,我对C ++还是陌生的.任何帮助将不胜感激.

Maybe theres a better way to go about doing this? I should add that I am new to C++, well, C in general. Any help would be greatly appreciated.

推荐答案

您可能对逻辑有很多误解.如果您希望能够从作为第一个参数给出的文件名中进行读取,或者如果没有给出任何参数,则可以从stdin中进行读取,则可以创建一个函数,该函数从输入流中读取string并仅输出所有以a分隔的单词通过将istream引用传递给函数并传递打开的ifstreamstd::cin来分隔空格(专门处理第一个单词).

You may be overthinking the logic quite a bit. If you want to be able to read from the filename given as the first argument or from stdin if no argument is given, you can create a function that reads a string from the input stream and simply outputs all words separated by a space (handling the first word specially) by passing an istream reference to the function and either passing the open ifstream or std::cin.

一个简短的示例可能如下所示:

A short example could be something like the following:

#include <iostream>
#include <fstream>
#include <string>

void squishws (std::istream& in)
{
    bool first = true;
    std::string str;

    while (in >> str)   /* while words read */
        if (first) {    /* no space before 1st word */
            std::cout << str;
            first = false;
        }
        else            /* output remaining words with 1 space */
            std::cout << " " << str;

    std::cout << "\n";  /* tidy up with newline */

}

int main (int argc, char **argv) {

    if (argc > 1) {     /* read from file if given as argument */
        std::ifstream f (argv[1]);
        if (f.is_open()) 
            squishws (f);
        else {
            std::cerr << "error: file open failed.\n";
            return 1;
        }
    }
    else {  /* read from stdin */
        squishws (std::cin);
    }

    return 0;
}

您可以根据需要添加输出文件引用,也可以直接在命令行上将输出重定向到输出文件.

You can add an output file reference, as required, or simply redirect the output to an output file on the command line.

示例输入文件

$ cat dat/wswords.txt
this       is         a

file    with


multiple

whitespace.

使用/输出示例

$ ./bin/file_rmwhitespace < dat/wswords.txt
this is a file with multiple whitespace.

这篇关于在C ++中删除多余的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆