hasing

May 28, 2014 at 2:47pm
So if I have "www.google.it", "74.125.225.127", and "www.microsoft.com", "64.4.11.42", and 8 other in a String called 'fact'. How do I create a code that will hash this and when the user input google.com, the program will output the numbers?

May 28, 2014 at 3:07pm
So, hashing? first of all, with witch algorithm of hashing do you want to make it hash? MD4? MD5? etc. ?
OR with an algorithm by yourself?
after making the algorithm of hashing other part of program is so easy.
May 28, 2014 at 3:22pm
by myself..
May 28, 2014 at 3:32pm
Use std::map:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
#include <map>

int main( void )
{
  std::map< std::string, std::string > ips;
  ips[ "www.google.it" ] = "74.125.225.127";
  std::cout << ips["www.google.it"] << std::endl;
  return 0;
}
May 28, 2014 at 4:01pm
as the LowestOne said you should use map, but before it you should hash them with your algorithm, then as the LowestOne said take them to the map and now you can use them.
I think i can't understand your question. if its true :( sorry :S
Have Nice Life
May 28, 2014 at 6:11pm
In addition to what the lowestOne suggested, you can also try std::unordered_map which actually hashes the values to provide O(1) queries.
std::map uses a red_black tree internally to store values and so queries are closer to O(lgN) than O(1) (This is in the worst case ofc, so choose wisely)

http://www.cplusplus.com/reference/unordered_map/unordered_map/unordered_map/

You can also provide unordered_map with a hash function to use rather than relying on the internal hash function.
Jun 4, 2014 at 1:56pm
What if I have a huge number of websites let say like 500, how do I do it then using <map>?
Jun 4, 2014 at 2:18pm
Exactly the same way.
If you get your Site→IP relation from, say, external file in format
site1 ip1
site2 ip2
You can do following:
1
2
3
4
5
6
std::string site;
std::string IP;
std::unordered_map<std::string, std::string> ips;
std::ifstream in("hosts");
while(in >> site >> IP)
    ips[site] = IP; //or in.insert(site, IP) 


Last edited on Jun 4, 2014 at 3:19pm
Jun 4, 2014 at 2:40pm
#include <iostream>
#include <string>
#include<fstream>
# include <sstream>
#include <stdlib.h>
#include <map>

using namespace std;
int main()
{
ifstream infile;
infile.open("URL");

string header;
getline (infile, header);

map<string, string> ips;
string site;
string IP;
while (infile>>site>>IP)
{
cout<<" Enter a URL"<<endl;


AM I doing this right? How do I get the program to get the right URL?
LIke if I put google.com as "cin"
Jun 4, 2014 at 2:46pm
Are you getting list of URL→IP relations from file or standard input (std::cin)?
Jun 4, 2014 at 2:53pm
Yes I am, the file contains list of URL and theri IP address
So it looks like
"www.google.com", "129.100.56.68",
Jun 4, 2014 at 3:20pm
So it will be:
1
2
3
4
5
6
7
8
9
10
11
while(std::getline(in, site, ',') && std::getline(in, IP, ',')) {
    //Here you should strip quotes from string  you got
    ips[site] = IP; //or in.insert(site, IP)
}
std::cout << "Enter a URL: ";
std::cin >> URL;
if(ips.find(URL) != ips.end())
    std::cout << ips[site];
else
    std::cout << "site not found";
std::cout << std::endl;
Last edited on Jun 4, 2014 at 3:21pm
Jun 4, 2014 at 3:29pm
Still not working.. saying website not found.


Last edited on Jun 4, 2014 at 3:57pm
Jun 4, 2014 at 3:35pm
One obvious problem is that your data has quotes around it. When you enter the URL are you entering it with quotes? Can you remove the quotes from the data file? Otherwise I would suggest removing the quotes when you read in the data.

Second problem I see is this line:
 
while(getline(infile, site, ',') && getline(infile, IP, ','))


After the second getline, the infile buffer is going to contain a '\n'. That is going to terminate subsequent reads so you will get only the first pair in your map. You need to use the following in order to skip the '\n'.
 
infile.ignore (1000, '\n');



PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Last edited on Jun 4, 2014 at 3:40pm
Jun 4, 2014 at 3:38pm
is there any way I can do it without removing the quotes?
Jun 4, 2014 at 3:51pm
even with adding the quotes and adding the \n it's still saying site not found
Jun 4, 2014 at 3:52pm
is there any way I can do it without removing the quotes?
Enter site to search for with quotes. Or add them to string after you enter it. like: string = "\"" + string + "\"";

infile.ignore (1000, '\n');
I suggest to use in >> ws to skip whitespace characters. Also it helps because there is whitespace between comma and IP.
Like:
1
2
3
4
while(getline(infile, site, ',') >> std::ws && getline(infile, IP, ',')) {
    infile >> std::ws;
    //rest of loop
}
Topic archived. No new replies allowed.