读取文件并删除重复字母

发布于 2024-08-11 09:20:42 字数 1561 浏览 1 评论 0原文

所以我的目标是创建一个函数,将部分填充的字符数组作为形式参数,并从数组中删除所有重复的字母。所以我只需要读取一个 .txt 文件,其内容类似于“11 ABC abca A ggt”,然后让程序吐出“ABC abcg t”

到目前为止,我的程序吐出“1 ABC abc”

我会非常感谢对此的任何帮助。

这是我所拥有的...

#include <iostream>
#include <fstream>
using namespace std;

bool deleterepeat( char arraytocheck[], char lettertocheck, int length)
{
    bool onlistflag = false;
    {
    for (int i = 0; i < length; i++)
    {
        if (arraytocheck[i] == lettertocheck)
        {
            onlistflag = true;
        }
    }
    }
    return onlistflag;
}



int main()

{       
    const int MAX = 15;
    char inFile[MAX];
    char clearedList[MAX];
    int clearedlength = 0;

        cout << "Choose a file: ";
        cin.getline(inFile, 15);
        ifstream in(inFile);

    if(!in) {
        cout << "Cannot open input file.\n";
        return 1;
    }


    while(in) {
        in.getline(inFile, MAX);


        for (int i = 0; i < MAX; i++)
        {
            in >> inFile[i];
        }
        for (int i = 0; i < MAX; i++)
        {
            if (deleterepeat(clearedList, inFile[i], i) == false)
            {
                clearedList[clearedlength] = inFile[i];
                clearedlength++;
            }
        }

        for (int i = 0; i < clearedlength; i++)
        {
            cout << clearedList[i] << " ";
        }



        if(in) cout << inFile << endl;
    }

    cout << endl;
    cin >> inFile;

    in.close();

    return 0;
}

So my goal is to make a function that has a partially filled array of characters as a formal parameter and deletes all repeated letters from the array. So I just need to read a .txt file with it's contents as something like "11 A B C a b c a A g g t " and have the program spit back out "A B C a b c g t"

As of now my program spits back "1 A B C a b c "

I'd really appreciate any help on this.

Here is what I have...

#include <iostream>
#include <fstream>
using namespace std;

bool deleterepeat( char arraytocheck[], char lettertocheck, int length)
{
    bool onlistflag = false;
    {
    for (int i = 0; i < length; i++)
    {
        if (arraytocheck[i] == lettertocheck)
        {
            onlistflag = true;
        }
    }
    }
    return onlistflag;
}



int main()

{       
    const int MAX = 15;
    char inFile[MAX];
    char clearedList[MAX];
    int clearedlength = 0;

        cout << "Choose a file: ";
        cin.getline(inFile, 15);
        ifstream in(inFile);

    if(!in) {
        cout << "Cannot open input file.\n";
        return 1;
    }


    while(in) {
        in.getline(inFile, MAX);


        for (int i = 0; i < MAX; i++)
        {
            in >> inFile[i];
        }
        for (int i = 0; i < MAX; i++)
        {
            if (deleterepeat(clearedList, inFile[i], i) == false)
            {
                clearedList[clearedlength] = inFile[i];
                clearedlength++;
            }
        }

        for (int i = 0; i < clearedlength; i++)
        {
            cout << clearedList[i] << " ";
        }



        if(in) cout << inFile << endl;
    }

    cout << endl;
    cin >> inFile;

    in.close();

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一萌ing 2024-08-18 09:20:42

首先使用 sort 算法对数组进行排序,然后使用独特算法。

下面是一个示例函数,它接受一个字符串(从文件中读取的一行)并返回一个包含所有唯一字符的字符串:

string getUniqueCharacters( string s )
{
    sort( s.begin(), s.end() );
    string::iterator newEnd = unique( s.begin(), s.end() );
    return string( s.begin(), newEnd );
}

对于输入字符串 11 ABC abca A gg t,上述函数生成 1ABCabcgt(请注意,空格字符的处理方式与任何其他字符相同)。

该函数的复杂度为 O(n * log n),因此即使对于长字符串,它仍然相当快。此外,如果字符串中的字符超过 256 个(考虑 unicode),此算法也适用。只需将 string 更改为 wstring 即可完成。

Sort your array first using the sort algorithm, then remove all adjacent duplicates using the unique algorithm.

Here's an example function which takes a string (a line read from your file) and returns a string with all the unique characters:

string getUniqueCharacters( string s )
{
    sort( s.begin(), s.end() );
    string::iterator newEnd = unique( s.begin(), s.end() );
    return string( s.begin(), newEnd );
}

For the input string 11 A B C a b c a A g g t, the above function yields 1ABCabcgt (note that the space character is treated like any other character).

This function has O(n * log n) complexity, so it's still reasonably fast even for long strings. Also, this algorithm also works if you have more than 256 characters (think of unicode) in your strings. Just change string to wstring and you're done.

飘过的浮云 2024-08-18 09:20:42
#include <iostream>
#include <fstream>
using namespace std;

int main() {       
    string ins, outs; // resizable

    cout << "Choose a file: ";
    cin >> ins;
    ifstream in(ins);

    if(!in) {
        cout << "Cannot open input file.\n";
        return 1;
    }

    while(in >> ins){
        for(string::const_iterator it = ins.begin(); it != ins.end(); ++it){
            if(outs.find(*it, 0) == string::npos){
                outs.append(1, *it);
            }
        }
    }
    in.close();

    cout << outs << endl;

    return 0;
}

我认为你正在尝试做的事情。但如果你知道你在工作
使用ascii(通常仍然是一个合理的假设),你可以戏剧性地
提高性能(从二次到线性时间,如果 str.find() 是线性的)
保留数组 bool saw[256] 并使用 if(seen[*it]) 而不是
搜索。或者,更可扩展的是,引入 STL 地图容器,它可以让您找到以下列表:
任意长度的唯一字符串。

#include <iostream>
#include <fstream>
using namespace std;

int main() {       
    string ins, outs; // resizable

    cout << "Choose a file: ";
    cin >> ins;
    ifstream in(ins);

    if(!in) {
        cout << "Cannot open input file.\n";
        return 1;
    }

    while(in >> ins){
        for(string::const_iterator it = ins.begin(); it != ins.end(); ++it){
            if(outs.find(*it, 0) == string::npos){
                outs.append(1, *it);
            }
        }
    }
    in.close();

    cout << outs << endl;

    return 0;
}

is what I think you were trying to do. But if you know you're working
with ascii (still often a reasonable assumption) you can dramatically
improve performance (from quadratic to linear time, if str.find() is linear) by just
keeping an array bool seen[256] and using if(seen[*it]) instead of a
search. Or, more extensibly, bringing in the STL map container, which would let you find a list of
unique strings of arbitrary length.

蓝色星空 2024-08-18 09:20:42
std::string f(istream & is) {
    bool known[256] = { false };
    char ch;
    std::string result;
    while (is >> ch) {
        if (! known[ch] /* || std::ispace(ch) */) {
            result += ch;
            known[ch] = true;
        }
    }
    return result;
}

或者(未经测试)

struct NotAgain {
    NotAgain() {
        std::fill_n(&known_[0], 256, false);
    }
    bool operator()(char ch) {
        const bool r = known_[ch];
        known_[ch] = true;
        return r;
    }
private:
    bool known_[256];
};

void f(std::string & buffer) {
    buffer.erase(
            std::remove_if(
                buffer.begin(),buffer.end(),
                NotAgain()),
            buffer.end());
}

或者可以很容易地直接在stream_iterator上使用。这个想法总是一样的,有一个包含 256 个元素的数组来记住已经见过的字符。当然,它也适用于角色,因为角色数量较少且数量有限。该解决方案不会扩展太多。如果唯一元素数量较多,则必须考虑关联映射(std::map(树)/std::unordered_map(散列))

std::string f(istream & is) {
    bool known[256] = { false };
    char ch;
    std::string result;
    while (is >> ch) {
        if (! known[ch] /* || std::ispace(ch) */) {
            result += ch;
            known[ch] = true;
        }
    }
    return result;
}

Or (untested)

struct NotAgain {
    NotAgain() {
        std::fill_n(&known_[0], 256, false);
    }
    bool operator()(char ch) {
        const bool r = known_[ch];
        known_[ch] = true;
        return r;
    }
private:
    bool known_[256];
};

void f(std::string & buffer) {
    buffer.erase(
            std::remove_if(
                buffer.begin(),buffer.end(),
                NotAgain()),
            buffer.end());
}

Or it can be easily used directly on stream_iterator. The idea is alway the same, have an array of 256 elements that remembers the characters already seen. Or course, it works well with characters as there is a small and limited number of them. This solution won't scale much. With bigger number of unique elements, you'll have to consider associative maps (std::map (tree) / std::unordered_map (hash))

望她远 2024-08-18 09:20:42

这是一个简单的东西,我可以给你这样做的过程......

1.从输入数组中读取一个字符。

2.检查输出数组中是否存在。

3.如果不是,则将其插入到输出数组,对输入数组的所有字符重复该过程。

It is a simple stuff , i can give u process to do so....

1.Read a char from input array .

2.check whether it exists in output array.

3.if not then insert it to output array , repeat the process for all chars of input array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文