Is there any way in pure C to implement set?

Asked

Viewed 108 times

5

I’m doing a job for college and I need to check word for word from a "dictionary" on file txt and compare these words to the words of an array that has been provided.

To make my life easier (because I will have to find some way to make my program faster), is there, in C, the possibility of using set type variables? For example, the Python "sets". Or I’ll have to do everything "in hand" anyway?

  • c++ provides mathematical set operations for your containers in a generic way. See an example to calculate difference using std::set_difference: http://en.cppreference.com/w/cpp/algorithm/set_difference. If you decide to change the question tags to c++ let me know via comment that I reply with usage examples explaining.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

3 answers

2

Not directly, you will have to implement the data structure.

A library that already implements this is the Gnulib, but until you make it work you’ve turned around. It’s only worth it for more intensive use, not to break a branch.

0

I believe that in C you cannot use functions like the ones Python Set allows, comparisons should be done "manually", you could use an array to store all words and compare through a loop of repetition.

If you are comparing only one information with another information you can use a simple vector, but if you need to compare more than one information at the same time, you can make a struct and make an array of structs.

-3

You can do it in C++, change the extension to .c for .cpp and use the compiler g++ instead of the gcc. You can use the vector, set, queue, map, ...
have several, take a look at: http://pt.cppreference.com/w/cpp/container

Example:

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> numbers;

    numbers.push_back(42);
    numbers.push_back(314159); 

    for (int i : numbers) { // c++11 range-based for loop
        std::cout << i << '\n';
    } 

    return 0;
}
  • 2

    I "think" that the question is C and not C++...

  • 1

    And this example does not use set.

  • I found the answer valid, many times people use C by not knowing the facilities of C++, if he wants something python style should use something more modern type c++, he does not use set in the example but as he said in the description can be adapted to: vector, set, Queue, map

Browser other questions tagged

You are not signed in. Login or sign up in order to post.