[RESOLVED] c++ dictionary - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 4 FirstFirst 1234 LastLast
Results 16 to 30 of 46

Thread: [RESOLVED] c++ dictionary

  1. #16
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: c++ dictionary

    Usually the stack is used to store the contents of local variables and the heap is used for dynamic memory allocation (such as used for set, map, vector, and when new is used to allocate memory). The size of the stack space is specified when the program is linked. The size of the heap is the available memory. Much more memory can be obtained from the heap than from the stack. When large data structures are needed, they almost always need to use heap memory rather than stack memory.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  2. #17
    Join Date
    May 2017
    Posts
    175

    Re: c++ dictionary

    can you please demonstrates what you said on the code ?? SO I can understand . thanks

  3. #18
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: c++ dictionary

    Error C2371 'dictionary': redefinition; different basic types Project
    Code:
    ifstream  dictionary ("Napster.txt");
    ...
    set <string> dictionary;

    Yep, the compiler is correct!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  4. #19
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: c++ dictionary

    Quote Originally Posted by david16 View Post
    can you please demonstrates what you said on the code ?? SO I can understand . thanks

    Demonstrate what?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  5. #20
    Join Date
    May 2017
    Posts
    175

    Re: c++ dictionary

    I'm in a void function do I have to remove it and simply plug it in main ?? I have an error if I use void function for my code you see the code ?


    Code:
    void dict() {
    
    	ifstream  dictionary ("Napster.txt");
    	
    	if ( !dictionary ) {
    
    		cout << "\nCannot open file.\n " << endl;
    	}
    	
    	set <string> dictionary;
    	string read;
    
    }
    and in main we have the call dict ();
    Last edited by david16; July 11th, 2017 at 01:29 PM.

  6. #21
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: c++ dictionary

    Quote Originally Posted by david16 View Post
    I;m in a void function do I have to remove it and simply plug it in main ?? I have an error if I use void function for my code you see ?
    No, I don't see. ??? You'll need to post the program code.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  7. #22
    Join Date
    May 2017
    Posts
    175

    Re: c++ dictionary

    did post it post #20 its a void function you see do I have to change something in it??

    I'm getting an error on set < string> dictionary;

    Error C2371 'dictionary': redefinition; different basic types Proje 47
    Last edited by david16; July 11th, 2017 at 01:29 PM.

  8. #23
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: c++ dictionary

    Quote Originally Posted by david16 View Post
    did post it post #20 its a void function you see do I have to change something in it??

    I'm getting an error on set < string> dictionary;

    Error C2371 'dictionary': redefinition; different basic types Proje 47
    See post #18.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  9. #24
    Join Date
    May 2017
    Posts
    175

    Re: c++ dictionary

    its like this in my code ??? why is it wrong they should be seperated is that what you mean ?/ I should change the names like that right ???

    Code:
    void dict () {
    
    	ifstream  dictionary ("Napster.txt");
    	
    	if ( !dictionary ) {
    
    		cout << "\nCannot open file.\n " << endl;
    	}
    	
    	set <string> dicts;
    	string read;
    
    }
    I have an error when I compile that says cannot open showing the location of my file that means did work ??
    Last edited by david16; July 11th, 2017 at 02:21 PM.

  10. #25
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: c++ dictionary

    You can't define the same variable more than once in the same block.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  11. #26
    Join Date
    May 2017
    Posts
    175

    Re: c++ dictionary

    Code:
     void dict() {
    
    	ifstream  dictionary("Napster.txt");
    
    	if (!dictionary) {
    
    		cout << "\nCannot open file.\n " << endl;
    	}
    
    	string read;
    
    	while (getline(dictionary, read))
    	{
    		// Process read
    	}
    }
    this is my updated code I did compile it worked and did call it to main . you think I should add dictionary.close (); and dictionary.clear (); when I finish what I should do inside the function ?? my task it to go throught all the file and search if any word in my 2d array that was filled with random characters matches a word if yes it should cout that word you see ??
    Last edited by david16; July 11th, 2017 at 03:58 PM.

  12. #27
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: c++ dictionary

    There's no need to do an explicit close for dictionary as the file will be closed automatically when dictionary goes out of scope.

    Inside the while loop IMO you should store the read word into a set which should be passed as a parameter to the function.

    my task it to go throught all the file and search if any word in my 2d array that was filled with random characters matches a word if yes it should cout that word you see
    The easiest way of doing this is to use a map because you want a count. What you want to do is to have an algorithm that produces a potential word from the 2d array and then test if that is actually a word in the dictionary.

    Consider a program something like
    Code:
    #include <string>
    #include <map>
    #include <set>
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    const string dictnam = R"(c:\downloads\113809of.fic)"s;
    
    bool dict(set<string>& dicset)
    {
    	dicset.clear();
    
    	ifstream stdict(dictnam);
    
    	if (!stdict.is_open())
    		return false;
    
    	string line;
    
    	while (getline(stdict, line))
    		dicset.insert(line);
    
    	return true;
    }
    
    bool nextword(string& word)
    {
    	//code to get next word from 2d array to check if actual word
    	//for testing, just returns words from array warr
    
    	static const string warr[] = { "bird", "notfly", "plane", "bird" };
    	static const size_t nowd = sizeof(warr) / sizeof(warr[0]);
    
    	static size_t wdnum = 0;
    
    	if (wdnum >= nowd)
    		return false;
    
    	word = warr[wdnum++];
    	return true;
    }
    
    int main()
    {
    	set<string> dicset;
    
    	if (!dict(dicset)) {
    		cout << "Cannot open dictionary " << dictnam << endl;
    		return 1;
    	}
    
    	string word;
    	map<string, int> wrdmap;
    
    	for (bool more = true; more; more = nextword(word))
    		if (dicset.count(word))
    			++wrdmap[word];
    
    	cout << "The found words are" << endl;
    
    	for (const auto& w : wrdmap)
    		cout << w.first << " " << w.second << endl;
    
    }
    Last edited by 2kaud; July 11th, 2017 at 06:43 PM. Reason: Code change
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  13. #28
    Join Date
    May 2017
    Posts
    175

    Re: c++ dictionary

    Great . I guess I should start my task of searching in while loop now . If I have huge amount of words that means program will get slower in finding a word in the array right ?? I have 511131 words in my text file
    Last edited by david16; July 11th, 2017 at 04:52 PM.

  14. #29
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: c++ dictionary

    Quote Originally Posted by david16 View Post
    Great . I guess I should start my task of searching in while loop now . If I have huge amount of words that means program will get slower in finding a word in the array right ?? I have 511131 words in my text file
    Not really. See code in post #27. The order of complexity of a map is (log size) for a find where size is the number of elements in the map. This is because map is based upon a tree and find is based upon a binary search as elements in the tree are stored in ascending sorted order.

    Your code doesn't actually search the map. You use .find() to find the word or not. This simplifies the program - and lets you concentrate on the required task which is to determine all required permutations of letters from the array. For each obtained letter permutation from the array, determining if this is a word or not is trivial!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  15. #30
    Join Date
    May 2017
    Posts
    175

    Re: c++ dictionary

    with a map is the program capable of getting a word in all 8 directions ?? such as in a crossword game ??

Page 2 of 4 FirstFirst 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)