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.
Related
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 3 years ago.
Improve this question
I am trying to read the documentation of the DOLFIN c++ library for finite element modelling located on this link:
https://fenicsproject.org/olddocs/dolfin/1.3.0/python/programmers-reference/index.html
but the documentation is hard to read, so for someone without c++ knowledge how you will read the following specification of parameters for the c++ method eval_cell() of the Expression class (https://fenicsproject.org/docs/dolfin/2017.2.0/python/programmers-reference/cpp/function/Expression.html):
Parameters:
double > & values (Array<) – (Array<double>) The values at the point.
Array< double > & x (const) – (Array<double>) The coordinates of the point.
ufc::cell & cell (const) – (ufc::cell) The cell which contains the given point.
After taking a look at the page t.niese linked in the comments I think this is a automatically generated documentation, with a really bad generator (like really really bad).
So, if we fix the butchered first line, realign some braces here and there and fix the position of const it might become clearer:
Parameters
const Array<double>& values1 – The values at the point.
const Array<double> &x – The coordinates of the point.
const ufc::cell &cell – The cell which contains the given point.
Meaning
You are dealing with a function that takes three parameters, the first and second are of type Array<double>, which seems to be generic container. The third parameter is of type ufc::cell, whatever this is. All three parameters are passed by reference (see the & before each variable name) and not by value. But they are not just passed as reference but actually as const reference (see the const), meaning that the function can't modify the objects you give to it.
I can't however say much about the comments for each parameter.
1 I assume the first parameter is also const, because it got the brackets, where the const is noted in the other two parameters, but this is just guessing.
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.
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'm getting the error 'term does not evaluate to a function taking 1 arguments' on the following line in my code:
steerToSpiralRefPoint(m_CentrePos);
But I'm not sure why this is... Is it that the function will only take 1 argument as a parameter, but since the 'm_centrePos' variable holds more than 1 attribute, the function is effectively receiving several parameters?
I have defined 'steerToSpiralRefPoint' with the line:
CDirectOverflyItem steerToSpiralRefPoint = new CDirectOverflyItem::SteerStep(const CHeloData aHeloData);
'm_CentrePos' has been assigned the value 'cCentrePos' at the start of this file ('cCentrePos' is a variable of type 'CCoordinate', which has a number of attributes- latitude, longitude, altitude, etc).
'CDirectOverflyItem's also has a number of attributes- ground speed, wind speed, wind angle, etc.
Can anyone point out to me why I'm getting this error, and how I should correct it?
This expression
steerToSpiralRefPoint(m_CentrePos);
is a postfix expression of a function call. However as it follows from your post steerToSpiralRefPoint is not a function (or function pointer) but a pointer to an object. If you want to assign a value to the pointer then you have to write
steerToSpiralRefPoint = m_CentrePos;
Or if there is an operator function for this type then the code should look as
( *steerToSpiralRefPoint )( m_CentrePos );
And this construction
CDirectOverflyItem steerToSpiralRefPoint = new CDirectOverflyItem::SteerStep(const CHeloData );
is also invalid. You may not use qualifiers before variables in expressions. They may be used only in declarations.
It seems that the issue was that I was trying to pass the wrong data type into the parameter- it was expecting a 'CHeloData', but I was trying to give it a 'CCoordinate'.
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
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