web::json::value obj;
obj[JSONKeyRequest] = web::json::value::string(JSONValueRequest);
I create a JSON value, and insert some key and values to it.
Then I get this obj in another function, trying to check that whether obj[JSONKeyRequest] equals to "abc", but it doesn't work:
web::json::value getObj = this->GetSendObj();
if (getObj[JSONKeyRequest] == web::json::value::string(L"abc"))
{
}
However, VC keeps shows:"Errors:No operators [] matches this operands , operand types are const web::json::value[std::wstring]
So, how could I get the value based on the key and compare the value with a string ?
First of all, the error tells you what exactly the arguments should be.
json requires wstring arguments, or if not wstring, you can input literals.
obj[L"JSONKeyRequest"] = web::json::value(L"JSONValueRequest");
should work.
Secondly, for comparison try declaring a wstring first and then comparing like
wstring temp = "abc";
if (getObj["JSONKeyRequest"] == temp)
{
}
This should work.
if (getObj.at(key) == web::json::value::string(L"abc"))
This one works for me.
Related
Short Description
I am trying to compile a short lambda to convert a giving input to string format. My overall class uses a template, i.e template <typename T>. I want to overload << to print-out an object which can be of any type; i.e, int, string, etc. For example, let's say that my object is {int age: 12, int* left: null, int* right: null}, I should be able to print something like "{value: "12", left: undefined, right: undefined}". In addition, if object = {string comment: "Distance learning....", int* left: undefined, int* right: undefined}, I should print out, "{value: "Distance learning....", left: undefined, right: undefined}". Below is a copy of the lambda function mark-up to convert from any data-type to a string.
std::function<std::string(T)> toString; //declares the blueprint for the lambda function, notice that I am returning a string, and expecting an object of type T, which can be of any type since i'm using a template.
toString = [](T value) -> std::string { //Begins the the body of the function
if (typeid(std::string).name() == typeid(T).name())
{ // not the best way to do this, but here I am checking that the input is already a string, in which case I do not need to convert or anything.
return value; //if the input 'value' is already a string, simply return it.
}
//do some stuff to convert the date into a string
return; // return the final 'value' in string format.
};
~Sorry in advance if my comments were confusing.
Problem
The idea works on paper, however, the problem happens when I have a data-type that is not of type string. Let's say that T == int, walking through the code, the if statement will be skipped, assuming that my condition is set-up correctly, and I should move down. This means that I will not return an int when the function blueprint says that I will return a string.
However, when the compiler goes through my code, it reads it and thinks that I am trying to send-back an int when the function is supposed to send back a string and it throws an error. "no viable conversion from returned value of type 'int' to function return type 'std::string'"
for example,
//Let T = int, value = 12, so the below parameter equals, int value, which holds the value of 12.
toString = [](T value) -> std::string {
if (typeid(std::string).name() == typeid(T).name())
{ //should failed since typeid(int).name != typeid(std::string).name
return value;
}
//do some stuff to convert the date into a string
return; // return the final 'value' in string format.
};
//However, when the compiler parses the code, it thinks that I am trying to return value, which is of type int. But the function is supposed to return a string.
//In reality, this should not happen since my if statement will take care of it.
Does anyone know a work around for this or any idea on how to fix my logic?
You need at least C++17 to use if constexpr.
With if constexpr you can write something as
if constexpr ( std::is_same_v<T, std::string> )
return value
else
{
// something else that return a std::string
}
Only this way you can exclude from the compilation return value, when T isn't std::string, and the body for the else case, otherwise.
Observe that I've used the std::is_same type-trait to check if T and std::string are the same type. std::is_same is something that the compiler decide compile time. Your typeid(...).value() is a run-time value. Suggestion: prefer compile-time type-traits, when possible.
But I need to see a more complete example to show how to use if constexpr in your class/struct.
Before C++17, you need two different functions to initialize tostring.
int i = ("aac" > "aab");
cout << i;
The above code does not give me the output as 1 (as it should be). But when I assign "aac" and "aab" to two separate string variables and use the variables instead of using strings directly (code attached below), I get the desired output.
Could anyone help me please?
string s1 = "aac";
string s2 = "aab";
int i = (s1 > s2);
cout << i;
Literal constants like "aac" aren't std::string objects; rather, they are just data in (read-only) memory that evaluate, in most 'access' cases, to the address of their first element (i.e. a char* pointer); so, a comparison between them will be a comparison between those addresses — something you are unlikely to be able to control or predict.
To get an inline comparison, in your case, you can use inline std::string constructors (sometimes knows as "wrappers"), like this:
int i=(string("aac")>string("aab"));
Or, using the more 'modern' "curly-brace" initializer syntax:
int i = (string{ "aac" } > string{ "aab" });
For more brevity, you can make use of the fact that std::string has versions of the > (and similar) operators that take a string literal as one of the arguments; thus, you need only 'wrap' one of the literals, and could reduce the above code to something like:
int i = (string{ "aac" } > "aab");
If you use C-style char * / char [] strings, you need to use strcmp like:
int i = strcmp("aac", "aab");
Otherwise, you are just comparing addresses of the first elements of both of strings.
I have a vector of strings and I want to compare the first element of the vector with a bunch of different "strings".
Here is what i wanted to do:
if (strcmp(myString[0], 'a') == 0)
but strcmp doesnt work. I basically want to check the contents of myString[0] with a bunch of different characters to see if there is a match. So it would be something like
if (strcmp(myString[0], 'a') == 0){
}
else if (strcmp(myString[0], 'ah') == 0){
}
else ifif (strcmp(myString[0], 'xyz') == 0)
etc..
What can i use to do this comparison? Compiler complains about "no suitable conversion from std:string to "constant char*" exists so i know it doesnt like that im doing a string to char comparison, but i cant figure out how to correctly do this.
std::string overloads operator== to do a string comparison, so the equivalent to
if (strcmp(cString, "other string") == 0)
is
if (cppString == "other string")
So your code becomes (for example)
else if (myString[0] == "ah")
'a' is not a string, it is a character constant. You need to use "a", "ah", "xyz", etc.
Also, if you want to use strcmp, you need to use:
if (strcmp(myString[0].c_str(), "a") == 0)
You can also use the overloaded operator== to compare a std::string with a char const*.
if (myString[0] == "a")
You have marked this post as C++.
compare the first element of the vector with a bunch of different
"strings".
If I am reading your post correctly, the first element of the vector is a std::string.
std::string has a function and an operator to use for string-to-string comparison.
The function is used like:
if (0 == pfnA.compare(pfnB))
As described in cppreference.com:
The return value from std::string.compare(std::string) is
negative value if *this appears before the character sequence specified by the arguments, in lexicographical order
positive value if *this appears after the character sequence specified by the arguments, in lexicographical order
zero if both character sequences compare equivalent
The operator==() as already described, returns true when the two strings are the same.
Below check is string and temp1->data is integer. I want to insert temp1->data into check. So I type cast int into const char*. This gives warning : cast to pointer from integer of different size [-Wint-to-pointer-cast]
Part of code:
temp1 = head;
std::string check;
check = "";
int i = 0;
while(temp1 != NULL)
{
check.insert(i, (const char*)temp1->data);// here is the warning
temp1 = temp1->next;
++i;
}
I want to know what other choices I have to insert the integer (temp1->data) into string(check) using insert function and what is the actual effect of warning [-Wint-to-pointer-cast] on my code.
Points:
data is integer, next is pointer to Node
I'm trying to implement a function to check if a linked list containing single digit number is palindrome or not. Yes, I know other methods for this but I just want to implement through this method too.
Here I want to store all the data of linked list into a string and directly check if the string is palindrome or not.
This question may seem duplicate of this . But it is not, here I explicitly asked for inserting integer into string using insert function contained in string class.
PS: on using std::to_string(temp1->data) gives me error ‘to_string’ is not a member of ‘std’.
You can use std::to_string function to convert integer to string and then insert it in a string using insert function on std::string.
std::string check;
check = "";
int i = 0;
check.insert(i, std::to_string(10));
The reason you are getting error "to_string is not a member of std" is may be because you did not include <string> header.
First, here's a way to convert an integer to a string without much work. You basically create a stream, flush the int into it, and then extract the value you need. The underlying code will handle the dirty work.
Here's a quick example:
stringstream temp_stream;
int int_to_convert = 5;
temp_stream << int_to_convert;
string int_as_string(temp_stream.str());
Here's more info on this solution and alternatives if you want to know more:
Easiest way to convert int to string in C++
Regarding the impact of the cast that you're doing, the behavior will be undefined because you're setting char* to an int value. The effect won't be converting the int value to a series of characters, instead you'll be setting the memory location of what the system interprets as the location of first character of a char array to the value of the int.
struct dic
{
string key;
int code;
};
dic H[71];
Now using key in the condition of the while-statement gives me an error.
while ((H[h].key)!= NULL)
{
}
The error I am getting is:
error: no match for 'operator!=' in 'H[h].dic::key != 0'
the type of dic::key is string, and you are trying to compare it to an integer (NULL == 0), which is not implemented. You need to check if the string is empty:
while (!H[h].key.empty()) {
...
}
The key of the element is a string. You can not compare a string to NULL as it is an object not a pointer. The macro NULL will most probably either defined as a pointer or an int value and neither of those is comparable to string.
The macro NULL is often defined to either 0 or (void *) 0, none of those values can be used when comparing to a std::string (unless, of course, you implement your own custom comparison operator, which you shouldn't).
If you want to check if a string is empty, use std::string::empty.
Maybe you wanted to say:
if (H[h].key.empty()) { ... }