hasing

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?

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.
by myself..
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;
}
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
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.
What if I have a huge number of websites let say like 500, how do I do it then using <map>?
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
#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"
Are you getting list of URL→IP relations from file or standard input (std::cin)?
Yes I am, the file contains list of URL and theri IP address
So it looks like
"www.google.com", "129.100.56.68",
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
Still not working.. saying website not found.


Last edited on
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
is there any way I can do it without removing the quotes?
even with adding the quotes and adding the \n it's still saying site not found
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.