dimanche 28 juin 2015

Too many types in declaration C++

When unsigned/signed long int a; is possible why unsigned/signed long float/double a; is not possible ?

Why do I get too many types in declaration error for the latter and not for the former ?

c++ unordered_map collision handling , resize and rehash

I have not read the C++ standard but this is how I feel like unordered_map in c++ suppose to work.

  • Allocate a memory block in the heap.
  • With every put request, hash the object and map it to a space in this memory
  • During this process handle collision handling via chaining or open addressing..

I am quite surprised that I could not find much about how the memory is handled by unordered_map. Is there a specific initial size of memory which unordered_map allocates. What happens if lets say we allocated 50 int memory and we ended up inserting 5000 integer?

This will be lot of collisions so I believe there should be kind of like a rehashing and re-sizing mechanism to decrease the number of collisions after a certain level of collision threshold is reached. Since they are explicitly provided as member functions to the class, I assume they are used internally as well. Is there a such mechanism?

Disjoint set data structure : track size of each tree

Below is my implementation to keep track of the size of each tree in the disjoint set forest.

Can you please tell me what is wrong with it ? I am trying to solve UVa problem https://goo.gl/ZiQCyH

#include <iostream>
#include <cstdio>
#include <unordered_map>
using namespace std;

class Node {
    public :
    int id;
    Node *parent;
    unsigned long long rank;



    Node(int id) {
        this->id = id;
       // this->data = data;
        this->rank =1; //size here
        this->parent = this;
    }
    friend class DisjointSet;
};

  class DisjointSet {
    unordered_map<int,Node*> nodesMap;

    Node *find_set_helper(Node *aNode) {

        if (aNode == aNode->parent) {
            return aNode->parent;
        }
        return find_set_helper(aNode->parent);
    }

    void link(Node *xNode,Node *yNode) {
        if( xNode->rank > yNode->rank) {
            yNode->parent = xNode;
            xNode->rank += yNode->rank;
        }
        // else if(xNode-> rank < yNode->rank){
        //     xNode->parent = yNode;
        //     yNode->rank += xNode->rank;
        // }
        else {
            xNode->parent = yNode;
            yNode->rank += xNode->rank;
        }
    }
public:
    DisjointSet() {

    }

    void AddElements(int sz) {
        for(int i=0;i<sz;i++)
            this->make_set(i);
    }

    void make_set(int id) {
        Node *aNode = new Node(id);
        this->nodesMap.insert(make_pair(id,aNode));
    }

    void Union(int xId, int yId) {
        Node *xNode = find_set(xId);
        Node *yNode = find_set(yId);

        if(xNode && yNode)
          link(xNode,yNode);
    }

    Node* find_set(int id) {
      unordered_map<int,Node*> :: iterator itr = this->nodesMap.find(id);
      if(itr == this->nodesMap.end())
          return NULL;

      return this->find_set_helper(itr->second);
    }



    ~DisjointSet(){
        unordered_map<int,Node*>::iterator itr;
        for(itr = nodesMap.begin(); itr != nodesMap.end(); itr++) {
            delete (itr->second);
        }
    }

};
int main() {

    int n,m,k,first,cur;

    //freopen("in.in","r",stdin);

    scanf("%d %d",&n,&m);

    while(n != 0 || m != 0) {

        DisjointSet *ds = new DisjointSet();
        ds->AddElements(n); // 0 to n-1

        //printf("\n n = %d m = %d",n,m);


        for(int i=1;i<=m;i++) {
            scanf("%d",&k);
            //printf("\nk=%d",k);
            if ( k > 0 ) {

                scanf("%d",&first);
                for(int j=2;j<=k;j++) {
                    scanf("%d",&cur);
                    ds->Union(first,cur);
                }
            }
        }

        Node *zeroSet = ds->find_set(0);
       // unsigned long long count = ds->getCount(zeroSet->id);
        printf("%llu\n",zeroSet->rank);
        delete ds;

        scanf("%d %d",&n,&m);
    }

    return 0;
}

The link function in the above code does the job of updating the tree size.

The solution to the problem is to find the set which elements 0 belongs to and get the size of the representative element of the set. But I am getting wrong answer with this code.

Can you please help me

DJI Onboard API QT Sample Program

I tried building the sample code on QT Creator that DJI released to test out on their drones, however, whenever I try building the sample code, it always gives me a bunch of errors and I'm not sure why. I've just begun using it so I don't know much about a lot of stuff and I just followed the directions on the manual that came with all the files so it'll be great if someone knew how to fix this problem

Error setting up SFML in Code::Blocks

I am a beginner in C++ and I have attempted to follow the instructions here to the letter for setting up SFML in code::blocks:

http://ift.tt/YFdHIm

After following all the instructions I still get the following error message:

enter image description here

I have no idea what this means. Could someone please explain in very basic terms what I need to do from here?

"libgcc_s_sjlj-1.dll" is missing

I am trying to run my first SFML application. I can run it via Code Blocks environment, but I can't run it through the Explorer - an error appears that says: "libgcc_s_sjlj-1.dll is missing". I added these lines to my linker options:

-static
-static-libgcc
-static-libstdc++

However, after trying to compile it, I see an error in the build log:

mingw32-g++.exe: error: unrecognized command line option '-static-libstdc++'

How can I fix it? My GCC version is 4.7.1 TDM-1

Code needed for decoding mp4 video using gstreamer?

Can anyone provide me C/C++ code for following command???

$ gst-launch-1.0 filesrc location=~/Desktop/test.mp4 ! decodebin name=dec 
 ! videoconvert ! autovideosink dec. ! audioconvert ! audioresample ! alsasink