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

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

Related

Reading automatically generated documentation for DOLFIN c++ library [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 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.

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.

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.

C/C++ code for rearranging .txt file [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 8 years ago.
Improve this question
Hi i have to make a function in C++ that gets one file as an input and as an output it has another file, that is rearranged in a specific way. In the file there are names of people,names of subject they are teaching, commented lines starting with '#' and also duplicated white spaces. The input file looks like this (before ':' are the names, after it are the subjects):
john : PA1 , PA2,OSY
#this is commented line
peter: PA1, PA2
And the output in the second file should look like this:
OSY: john
PA1: john, peter
PA2: john, peter
As you can see, the function should put one subject on each line (in alphabetical order) and then there should be ':' and after that the names of the people teaching it (also in aplhabetical order). Also all commented lines should be deleted and the same with duplicated white spaces.
I know how to delete duplicated white spaces and commented lines, but I have problem with rearranding the people and subjects. Is there any possible way to do this function without using classes?
I would be very thankful for any help or advices.
bool transform ( const char * inFile,
const char * outFile )
{
// todo
}
There are various different problems to solve. You must first identify those individual problems, then solve them one by one, and then put the pieces together. Many years ago, when I was a student at university, this approach was taught to me as "divide & conquer".
Here are the individual problems I can identify in your question:
Read a text file's contents into memory, so that you deal with strings and each line is a string.
Parse a string. Split it into substrings, so that you deal with a collection of strings (tokens). Know when a string starts with a particular character.
Create a data structure in which a sorted, unique string key relates to a list of (likewise ordered and unique) strings.
Write a series of strings to a text file.
Each of the sub-problems may well be individual separate questions on Stack Overflow. I'll give you hints for every one of them, so that you can google them or browse the archive for related questions:
Reading a text file line by line in order to end up with a series of std::string objects is best done with std::ifstream and std::getline.
Getting the first character of a std::string is easy: my_string[0], first checking if !my_string.empty(). Splitting it is more difficult. I would personally use the occasion and get started with the Boost libraries. See the Boost Tokenizer example.
The data structure you need is already there - C++ itself provides it as part of the language. The first thing which comes to mind is std::map<std::string, std::set<std::string> >.
Writing is simpler: use std::ofstream and write line by line with operator<<.
std::map and std::set are your best friends here.

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

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