std::map<std::string, enum> does not save values [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am writing a parser, and I decided to save type information in a std::map. When I use the [] operator to access it, I always get the 0 value for the enum. The map is declared as such:
enum type {Bool, Bool_a, Int_4, Inta_4, Int_8, Inta_8, Float_s, Floata_s, Float_d, Floata_d, Ch_s, Ch_a, Str, Invalid};
class kparse_ret{
...
std::map<std::string, type
...
} ret_data;
And is then set using something like
ret_data.type_list[itemname] = Int_4;
(Where itemname is a std::string)
The problem I'm having is that when I use
ret_data.type_list[data_name]
I always get Bool, or 0. (again, data_name is a std::string). I know that itemname and data_name have exactly the same contents when their respective contexts are reached. Furthermore, if I use itemname again to access it, I get the value I just set it to.

When comparing string as key for a map, not only the content of the string are compared but also the size of the the string for example.
The following will help you identify the problem :
std::map<std::string, std::string>
std::string key = "KEY";
std::string key2 = "KEY";
key2.resize(100);
now if you mapped something on "KEY" and use key2 to access it, you won't obtain the value yous expected.
jav

Related

no match for 'operator==' when using custom classes and std::find() [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have this class namescores_t which is basically a data structure containing a first/last name linked to a score. While filling a vector of namescores_t objects, I check to see if the object already exists like so:
// reading from file and inserting data...
vector<namescores_t> NS;
namescores_t ns(names, scores); // initializing namescores_t object with names & scores read from file
if(find(NS.begin(), NS.end(), ns) == NS.end()); // if object does not exist in the list, add it
NS.push_back(ns);
However, I am getting an error no match for 'operator==' (operand types are 'namescores_t' and 'const namescores_t'). when calling find(NS.begin(), NS.end(), ns) == NS.end()
What am I doing wrong?
I was under the assumption that std::find() was able to use operator< to determine equivalency, similar to std::map, but this is apparently not the case. So I simply overloaded the operator== in order to fix the issue.
bool namescores_t::operator==(const namescores_t &ns) const
{
// returns whether both the names and scores of the object are equal to one another
return (scores.get_mean() == ns.scores.get_mean() && name == ns.name);
}

Using unions and tags [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
How can we know whether the given input is int or char using unions in c++?
Please keep the program simple.
How can I actually know when the given input is char or int so that I perform specific operations?
I basically got to know that in a char array ,when I enter a double digits number,the array takes only the first digit,is there any correct method or I need to use only unions?
Unions are not able to distinguish what type you put in. When it is important (what it is in most cases) and you don't want to the union as cast, then you need to store what type of data you put in.
E.g.,
typedef union
{
int a;
char b[4];
} myUn;
now you can do
myUn mu;
mu.a=42;
char c=mu.b[0];
and your compiler wont complain. So you need to store in a further location what type you have put in (if important).
e.g.,
typedef struct
{
myUn mu;
int type;
}
and encode in type what you have put in. But that is just a very basic solution and you should ask why you want to use a union here.

what does this error mean in general? and how I fix it in this case? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
The error message appears on Xcode that says "invalid operands to binary expression.
in my code I'm using an array of a struct, i'm trying to sort input data in an ascending order, and i'm getting this error message at the "if" condition shown in the print screen at this link:
https://www.dropbox.com/s/0mch2gbxcif0a20/Screen%20Shot%202016-04-27%20at%2012.45.45%20PM.png?dl=0
The Code
if (studentsInfo[i] > studentsInfo[i + 1]) {}
The Error
Invalid operands to binary expression ('students' and 'students')
What do you compare in your program? As I see, you have to compare names, but all you do is compare an array element which is a struct data type.
If you are trying to compare names, you have to use dot "." operator to reach names. After yo compare names, you can change the elements's place.
The error means that > only takes two arguments and you are using it for something else. In this case you are comparing an entire data structure that does not have an override for > operator and is an undefined behavior. StudentsInfo[i] is a data structure that has more than one element in it. Replace the StudentsInfo[i] with StudentsInfo[i].GPA or another element whose data type has a defined > operator.

C++What is the meaning of "<" and ">" in this code snippet? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
This is an example from the e-book "Jumping into C++" by Alex Allain which I downloaded from here. On page 207 he has this code snippet:
#include <map>
#include <string>
using namespace std;
map<string, string> name_to_email;
My question is, please, what is the meaning of the last line, in particular the significance of the "<" and ">". Can the line be written map < string, string > name_to_email; i.e. must here be no spaces as I have inserted them?
That notation specifies the template parameters.
Can the line be written map < string, string > name_to_email; i.e. must here be no spaces as I have inserted them?
Spaces are fine.
My question is, please, what is the meaning of the last line, in particular the significance of the "<" and ">".
As #Cyber mentioned, they're template parameters. It's the way of using variable types in a C++. Rather than having a map for every type, like a StringToIntMap and a StringToCharMap and a CharToStringMap and a StringToStringMap etc. There's just a map, which can use any type. So a map<string, int> is essentially a map that takes a string as a key and maps it to an int as a value.
As Cyber noted, the notation specifies the template parameters. If you read this link to get an understanding of what the map is, you can see that when you are defining a map, you need to specify the two parts of the map. The key, and the value. In your example above you are creating a map of strings, that are accessed by a string key.
map<key, value>. So in another answer above, if you wanted to store integers, accessible by a string key, you would create a map like this map<string, int> lMyMap

How can I tell if a tuple is empty in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to know how I can identify if my tuple is empty?
Note that I use std::tuble in conjunction with std::map as below:
typedef std::tuple<SDESType,INT32,std::string> SDesItem;
typedef std::map<SDESType,SDesItem> SDesItemMap;
Now I want to check if the second value of SDesItemMap, that is a tuple, is empty or not?
if(SDesItems[SDESType] != NULL) // this is error
std::tuple acts like three values tied together. It has no predefined "empty" or "zero" state, other than empty states of each of its members. Empty value is always contextual, and in general means a value lying outside the domain of the regular type's values.
For example, a natural number can only be 0 (or 1, depending on your definition) upward. Thus, using an int gives you a convenient way to represent a value that's outside of the domain, and should be treated irregularly.
To represent that cleanly in your example, use optional<tuple<...>> as a map value (if you want to specifically state "there's an empty value at a given key"). This type states that the possible values of the type you get by instantiating the optional template are now all of the values of the original type, and a special, nothing (/empty/none) value.
You can also state that parts of the tuple can be empty (nothing), like
tuple <
std::string, // empty string ("") can be treated as "empty state"
optional<int>, // if you don't want to treat 0 (or -1) as "empty"
>
optional<> is not a part of std:: yet, so you have to write your own implementation or use the Boost one. Another way would be using value_ptr (Boost again), but here it wouldn't probably give you any benefits.