How to know if Key in Vector does not exist / If File has write permissions
Hi all,
I have 2 questions to ask, and I decided to ask them in a single thread (to save space for the other forum goers).
1. How to find if a key in a vector doesnt exist?
Ex. There is a hash_map Dictionary defined Dictionary[<string>, <string>] (meaning the key is represented as a “string”, and the value is also a string
If I have something like this:
hash_map <string, string> Dictionary;
string doesNotExist = “does not exist”
string DictValue = Dictionary[doesNotExist];
It should identify that the Key for that Dictionary does not exist.
It does not error however, but it returns a blank string "". I want to identify if the key does not exist since it may be possible that the key exists but also contains a blank string "".
2. How to find if a file is writable to or not?
The problem I have is the file can be opened if it has read permissions but it cannot be written to since it has no write permissions. I am imagining something like this:
If(myFile.isopen())
{
If(myFile.isWriteable)
{
//write to the file
}
Else
{
Printf(“Error: File is not writable”);
}
}
I tried searching but all I find is if the file exists, etc.
Thank you all for the future help!
Re: How to know if Key in Vector does not exist / If File has write permissions
Could you explain your first question in more detail ? For a dictionary, you
normally would use a map, not a vector. For a map, you would use the find()
member function.
Re: How to know if Key in Vector does not exist / If File has write permissions
Number 1 doesn't make sense to me. Vector's don't have keys and string DictValue = Dictionary[doesNotExist]; doesn't make sense syntactically.
2 you open the file for writing and check for failure. You could also call _access.
Re: How to know if Key in Vector does not exist / If File has write permissions
Hi Philip, and GCDEF, thanks for replying:
Oops, sorry, I got confused and made an error: it should be hash map and not vector. I declare it like this:
hash_map <string, string> Dictionary;
Hopefully this will clarify the first question. I will also edit my original post.
Thank you!
Re: How to know if Key in Vector does not exist / If File has write permissions
I have never used hash_map (not standard yet), but it must have a find function:
Code:
if (Dictionary.find("some_key") == Dictionary.end())
{
// key does not exist
}
Re: How to know if Key in Vector does not exist / If File has write permissions
Hi Philip,
Thanks for the tip! Going to try it.