-
July 11th, 2017, 12:02 PM
#16
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 11th, 2017, 12:04 PM
#17
Re: c++ dictionary
can you please demonstrates what you said on the code ?? SO I can understand . thanks
-
July 11th, 2017, 12:04 PM
#18
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 11th, 2017, 12:05 PM
#19
Re: c++ dictionary
Originally Posted by david16
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 11th, 2017, 12:06 PM
#20
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 12:29 PM.
-
July 11th, 2017, 12:15 PM
#21
Re: c++ dictionary
Originally Posted by david16
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 11th, 2017, 12:15 PM
#22
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 12:29 PM.
-
July 11th, 2017, 01:08 PM
#23
Re: c++ dictionary
Originally Posted by david16
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 11th, 2017, 01:10 PM
#24
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 01:21 PM.
-
July 11th, 2017, 01:14 PM
#25
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 11th, 2017, 01:25 PM
#26
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 02:58 PM.
-
July 11th, 2017, 03:44 PM
#27
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 05: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++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 11th, 2017, 03:46 PM
#28
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 03:52 PM.
-
July 11th, 2017, 04:26 PM
#29
Re: c++ dictionary
Originally Posted by david16
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 11th, 2017, 04:31 PM
#30
Re: c++ dictionary
with a map is the program capable of getting a word in all 8 directions ?? such as in a crossword game ??
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|