RapidJSON library getting a value inside an array by its index - c++

{"hi": "hellow",
"first":
{"next":[
{"key":"important_value"}
]
}
}
Accessing RapidJSON inside array:
this works: cout << "HI VALUE:" << variable["hi"].GetString() << endl; this will output: hellow as expected, the problem is to access inside values like if I want to get "Important_Value", I tried something like this: cout << "Key VALUE:" << variable["first"]["next"][0]["key"].GetString() << endl ; but this doesn't work, I want to be able to get the "important_value" by the first item of the array, and in this case it's the [0] that is causing error.
How do I do to get it by its index?
I hope it's clear my explanation.
Thanks in advance.

JSON
{"hi": "hellow", "first": {"next":[{"key":"important_value"} ] } }
Code:
rapidjson::Document document;
if (document.Parse<0>(json).HasParseError() == false)
{
const Value& a = document["first"];
const Value& b = a["next"];
// rapidjson uses SizeType instead of size_t.
for (rapidjson::SizeType i = 0; i < b.Size(); i++)
{
const Value& c = b[i];
printf("%s \n",c["key"].GetString());
}
}
Will print important_value

[Update]
By clever work of contributors, RapidJSON can now disambiguate literal 0 from string. So the issue is no longer happens.
https://github.com/miloyip/rapidjson/issues/167
The problem, as mjean pointed out, the compiler is unable to determine whether it should call the object member accessor or the array element accessor, by literial 0:
GenericValue& operator[](const Ch* name)
GenericValue& operator[](SizeType index)
Using [0u] or [SizeType(0)] can workaround this.
Another way to cope with this problem is stop using overloaded version for operator[]. For example, using operator() for one type of access. Or using normal functions, e.g GetMember(), GetElement(). But I do not have preference on this right now. Other suggestions are welcome.

I noticed this in the tutorial.cpp file;
// Note:
//int x = a[0].GetInt(); // Error: operator[ is ambiguous, as 0 also mean a null pointer of const char* type.
int y = a[SizeType(0)].GetInt(); // Cast to SizeType will work.
int z = a[0u].GetInt(); // This works too.
I didnt test it but you may want to try one of these;
variable["first"]["next"][0u]["key"].GetString()
variable["first"]["next"][SizeType(0)]["key"].GetString()

auto value = my_array[rapidjson::SizeType(index)].GetFoo();
// replace GetFoo with the type of element you are retrieving, e.g. GetString, GetObject

If you want to access it with brackets, then you can use the following:
int i=0;
cout<<"Key VALUE:"<<variable["first"]["next"][i]["key"].GetString()<<endl ;
Output: Key VALUE:important_value
It worked for me.

Related

For loop in C++ stops after a single iteration w/ pointer variable

So first all I'll preface this with: I just started using c++.
I have a structure that I store the pointer to in an unordered_map, setting members' values in the struct pointer as I get them through my process. Then I no longer need them in a map so I transfer then to a vector and loop through them.
Though on the second loop, it outputs my index (1) but the next statement of making a local pointer var for the struct at that index breaks it and the code terminates without any errors. since there are no errors then a try/catch doesn't give me anything either.
// Wanted to create a structure to handle the objects easier instead
// of multiple vectors for each property
struct appData {
std::string id = "";
std::string name = "";
std::string vdf_file = "";
std::string vdf_path = "";
};
// Relevant parts of my main()
int main() {
// Map that stores all the struct pointers
std::unordered_map<std::string, appData*> appDatas;
char memory[sizeof(appData)];
void* p = memory;
// New instance of appData
appData *tempAppData = new(p) appData();
tempAppData->appid = "86901";
// Add tempAppData to map with string key
appDatas["86901"] = tempAppData;
...
std::vector<appData*> unhashed_appDatas;
for (auto const& pair: appDatas) {
unhashed_appDatas.push_back(pair.second);
}
...
for (unsigned int x = 0; x < unhashed_appDatas.size(); x++) {
// Output index to see where it was messing up
std::cout << x << std::endl;
!! // This is where the issue happens on the second loop (see output)
appData *thisAppData = unhashed_appDatas[x];
std::string id = thisAppData->appid;
std::cout << id << std::endl;
/* ...
Do more stuff below
*/
}
...
return 0;
}
Terminal Output:
0 // Initial index of x
86901 // Id of first item
1 // New index of x on second loop before pointer var is created
// Nothing more is printed and execution terminates with no errors
My knowledge of c++ is pretty lacking, started it couple days ago, so the few things within my knowledge I've tried: moving the *thisAppData variable outside of the loop, using a for(var: vector) { ... }, and a while loop. I can assume that the issue lies with the pointer and the local variable when inside the loop.
Any help/input about how I could better approach this or if there's an issue with my code would be appreciated :)
Edit: Changed code to use .size() instead of sizeof() per #Jarod42 answer, though main issue persists
Edit2: Turns out it was my own mess-up, imagine that. 4Am brain wasn't working too well- posted answer regarding what I did incorrectly. Thanks to everyone who helped me
sizeof is the wrong tool here:
for (unsigned int x = 0; x < sizeof(unhashed_appDatas); x++) {
// ^^ wrong: give **static** size of the structure
// mainly 3 members (data, capacity, size), so something like `3*sizeof(void*)`
it should be
for (unsigned int x = 0; x < unhashed_appDatas.size(); x++) {
After many hours of trial and error I have determined the issue (aside from doing things in a way I should, which I've since corrected) it was something I messed up on that caused this issue.
TLDR:
Items wouldn't exist that I assumed did and tried to read files with a blank path and parse the contents that didn't exist.
Explaination:
In the first loop, the data I was getting was a list of files from a directory then parsing a json-like file that contained these file names and properties associated with them. Though, the file list contained entries that weren't in this other data file (since I had no check if they existed) so it would break there.
Additionally in the last loop I would get a member from a struct that would be the path of a file to read, but it would be blank (unset) because it didn't exist in data file so std::ifstream file(path); would break it.
I've since implemented checks for each key and value to ensure it will no longer break because of that.
Fixes:
Here are some fixes that were mentioned that I added to the code, which did help it work correctly in the end even if they weren't the main issue that I myself caused:
// Thanks to #EOF:
// No longer "using placement new on a buffer with automatic storage duration"
// (whatever that means haha) and was changed from:
char memory[sizeof(appData)];
void* p = memory;
appData *tempAppData = new(p) appData();
// To:
appData *tempAppData = new appData();
// Thanks to #Jarod42:
// Last for loop limit expression was corrected from:
for (unsigned int x = 0; x < sizeof(unhashed_appDatas); x++) {
}
// To:
for (unsigned int x = 0; x < unhashed_appDatas.size(); x++) {
}
// I am still using a map, despite comment noting to just use vectors
// (which I could have, but just would prefer using maps):
std::unordered_map<std::string, appData*> appDatas;
// Instead of doing something like this instead (would have arguably have been easier):
std::vector<std::string> dataKeys = { "1234" };
std::vector<appData*> appDatas = { ... };
auto indx = find(dataKeys.begin(), dataKeys.end(), "1234");
indx = (indx != dataKeys.end() ? indx : -1);
if (indx == -1) continue;
auto dataItem = appDatas[indx];
//
I appreciate everyone's assistance with my code

Logical error. Elements in std::string not replaced properly with for loop

I'm currently doing a programming exercise from a C++ book for beginners. The task reads as follows: "Write a function that reverses the characters in a text string by using two pointers. The only function parameter shall be a pointer to the string."
My issue is that I haven't been able to make the characters swap properly, see the output below. (And I also made the assumption that the function parameter doesn't count, hence why I'm technically using three pointers).
I am almost certain that the problem has to do with the for loop. I wrote this pseudocode:
Assign value of element number i in at_front to the 1st element in transfer_back.
Assign value of element number elem in at_back to element number i in at_front.
Assign value of the 1st element in transfer_back to element number elem in at_back.
Increment i, decrement elem. Repeat loop until !(i < elem)
I wasn't sure whether of not I was supposed to take the null terminator into account. I tried writing (elem - 1) but that messed up with the characters even more so I've currently left it as it is.
#include <iostream>
#include <string>
using namespace std;
void strrev(string *at_front) {
string *transfer_back = at_front, *at_back = transfer_back;
int elem = 0;
while(at_back->operator[](elem) != '\0') {
elem++;
}
for(int i = 0; i < elem; i++) {
transfer_back->operator[](0) = at_front->operator[](i);
at_front->operator[](i) = at_back->operator[](elem);
at_back->operator[](elem) = transfer_back->operator[](0);
elem--;
}
}
int main() {
string str = "ereh txet yna";
string *point_str = &str;
strrev(point_str);
cout << *point_str << endl;
return 0;
}
Expected output: "any text here"
Terminal window: "xany text her"
The fact that the 'x' has been assigned to the first element is something I haven't been able to grasp.
Here is the correct answer
void strrev(string *at_front) {
string *at_back = at_front;
char transfer_back;
int elem = 0;
while(at_back->operator[](elem) != '\0') {
elem++;
}
for(int i = 0; i <elem; i++) {
transfer_back = at_front->operator[](i);
at_front->operator[](i) = at_back->operator[](elem);
at_back->operator[](elem) = transfer_back;
elem--;
}
}
Let me explain why you have that error. string *transfer_back = at_front those two are pointed to the same reference, that is why when you change transfer_back->operator[](0) = at_front->operator[](i);this change will reflect in at_front string as well.
"Write a function that reverses the characters in a text string by using two pointers. The only function parameter shall be a pointer to the string."
This sounds to me like the question addresses C strings but not std::string.
Assuming my feeling is right, this could look like:
#include <iostream>
#include <string>
void strrev(char *at_front) {
char *at_back = at_front;
if (!*at_back) return; // early out in edge case
// move at_back to end (last char before 0-terminator)
while (at_back[1]) ++at_back;
// reverse by swapping contents of front and back
while (at_front < at_back) {
std::swap(*at_front++, *at_back--);
}
}
int main() {
char str[] = "ereh txet yna";
strrev(str);
std::cout << str << '\n';
return 0;
}
Output:
any text here
Live Demo on coliru
Note:
I stored the original string in a char str[].
If I had used char *str = "ereh txet yna"; I had assigned an address of a constant string to str. This feels very wrong as I want to modify the contents of str which must not be done on constants.
strrev():
The at_back[1] reads the next char after address in at_back. For a valid C string, this should be always possible as I excluded the empty string (consisting of 0-terminator only) before.
The swapping loop moves at_front as well as at_back. As the pointer is given as value, this has no "destructive" effect outside of strrev().
Concerning std::swap(*at_front++, *at_back--);:
The swapping combines access to pointer contents with pointer increment/decrement, using postfix-increment/-decrement. IMHO, one of the rare cases where the postfix operators are useful somehow.
Alternatively, I could have written:
std::swap(*at_front, *at_back); ++at_front; --at_back;
Please, note that std::string is a container class. A pointer to the container cannot be used to address its contained raw string directly. For this, std::string provides various access methods like e.g.
std::string::operator[]()
std::string::at()
std::string::data()
etc.

how to search for a string in a string array in c++

say i have the following:
string myArray[] = { "adam", "aaron", "brad", "brandon" };
cout << "Please type a name: ";
i want it so when a user types "bra" and hits enter, the program returns
brad
brandon
if the user types "a", the program returns
adam
aaron
if the user types "adam", the program returns
adam
I have tried strstr, mystring.compare(str), mystring.compare(x, n, str) - i can't find anything that is working.
what function would be the best way of handling this operation?
This is a great time for lambdas and std::copy_if. Assuming the string you want to find is named to_find:
std::copy_if(std::begin(myArray), std::end(myArray),
std::ostream_iterator<std::string>(std::cout, "\n"),
[&](const std::string& s){
return s.find(to_find) == 0;
});
Specifically, the way to test if some string a contains another string b, we can do:
a.find(b) == 0
std::string::find returns the index at which b is found, or npos if it's not found. We want 0, since you only want prefixes. We then wrap that in a lambda and pass it into copy_if, which will copy every element which passes our predicate and writes it to the output iterator we provide - in this case an ostream_iterator<std::string> which writes to std::cout and uses \n' as a delimeter.
To write out the same without C+11 would look something like:
const size_t size = sizeof(myArray) / sizeof(*myArray);
for (size_t i = 0; i < size; ++i) {
if (myArray[i].find(to_find) == 0) {
std::cout << myArray[i] << std::endl;
}
}
Depending on how big your list of names is going to be, it can quickly become very slow to scan through the whole thing. You might want to look into implementing your own prefix tree (aka trie).

C++ Map returning class object based on variable key

I reacently learned about map structures and I am trying to use one, but can't seem to solve one problem.
I have tried the code below:
map<string, valuePair> translator;
The class valuePair is just a combination of 2 objects (string and a number).
Im assigning values to the map
translator[currentWord] = valuePair(stateNo, "state");
Where currentWord is a variable string, stateNo is an int.
Now later I want to get back the valuePair number value from the map, but can;t seem to be able to do it.
Heres a screenshot of my watch window trying to access the variable x.
http://i.imgur.com/m3MOgi2.png
These are all the ways I managed to find online to return the value, yet none of them seem to work. As you can see the key "a" is in the map. What am I doing wrong?
[EDIT] Thanks guys, I used the tips you gave in comments and found out, that actually it works the way I expected - translator["a"].x prints the values I need. I have nothing to mark as "Correct answer" though, and I'm not sure what to do with this thread now :/
As Jonathan Henson posted in the comments, you would be better off posting your code than your debugger output. Ideally you want a minimal example that reproduces the error you are having.
Based on the debugger output, I'm wondering if you have a scope issue -- you are trying to access the map data outside of the scope where you have it defined. Without seeing the source code though, there is no way to know.
Here is a working example that does precisely what you are trying to do. The only modification is I have used a struct for valuePair, and c++ 11 initializer lists. This won't affect the map access code, but you might need to turn on c++ 11 support to compile it.
As a first step, look check it out in your debugger and see if you get the same difficulty. If so, your problem is the debugger or debugger setup, not your code.
If the debugger works for the sample code (posted below), gradually transform my code into your code (making minimal changes, building, and see if it still works). This is a very useful approach to learning the fine points of a language.
#include <iostream>
#include <map>
#include <string>
using namespace std;
struct valuePair
{
int num;
string str;
};
int main()
{
map<string, valuePair> translator;
translator["a"] = {0,"bla"};
translator["b"] = {1, "alb"};
translator["c"] = {2, "lab"};
valuePair res = translator["c"];
cout << "c:" << res.num << "," << res.str << "\n";
res = translator.at("b");
cout << "b:" << res.num << "," << res.str << "\n";
res = translator.find("a")->second;
cout << "a:" << res.num << "," << res.str << "\n";
return 0;
}
If you have an std::map<K,V> m; you can add elements and change values by using m[k] = v;. However the operator[] is an operation which always creates a key/value pair, if the key you are looking for is not contained in the map. Thus it is not allowed when you have a const reference or pointer, e.g. const std::map<K,V>&.
With a std::map you always have to consider the case that the key you are looking for is actually not contained in the map!
To look for the value stored under a given key you have to use std::map::find (link).
Example:
std::map<std::string,valuePair>::const_iterator it = translator.find(currentWord);
if(it != translator.end()) {
// the map contains an element with this key
const valuePair& value = it->second; // this is the value
}
else {
// the map *does not* contain an element with this key
}
As mentioned in the comments std::map::at (link) may be an alternative for C++11. But then you have to take care of the possible exception which is thrown when you use a key which does not exist in the map.
this works for me
#include <map>
#include <sstream>
#include <string>
int main()
{
std::map<std::wstring, double> items;
items[L"0"] = 0.123;
items[L"1"] = 1.234;
items[L"2"] = 2.234;
items[L"3"] = 3.345;
items[L"4"] = 4.567;
for (int i = 0; i < 5; i++)
{
std::wstringstream oss;
oss << i;
std::wstring key = oss.str();
double value = items[key];
}
return 0;
}

returning multiple strings from a function

I have a function where i want to be able return what is printed out so then i can print the output on the page. How will i return both of the string which are outputted within both of those if statements ?
std::vector<std::string> el;
split(el,message,boost::is_any_of("\n"));
std::string a ("");
for(int i = 0; i < el.size(); i++)
{
if(el[i].substr(0,3) == ".X/")
{
DCS_LOG_DEBUG("--------------- Validating .X/ ---------------")
std::string str = el[i].substr(3);
std::vector<std::string>st;
split(st,str,boost::is_any_of("/"));
boost::regex const string_matcher(splitMask[0]);
if(boost::regex_match(st[0],string_matcher))
{
a = "Correct Security Instruction";
}
else
{
a = "Incorrect Security Instruction"
}
boost::regex const string_matcher1(splitMask[1]);
if(boost::regex_match(st[1],string_matcher1))
{
a = "Correct Security screening result"
}
else
{
a = "Incorrect Security screening result"
}
return a;
}
}
Thankfull for any help :)
You can return an std::pair of strings, for instance.
Define a class with two appropriately-named string members and return an instance of that.
Then, start to think about what methods or other data would be useful to have on that class.
You can push the strings in a std::vector that is passed to the function as a reference and later iterate over the vector upon return.
I would return a std::pair of bool values (one to indicate if the instruction is correct, and one to indicate if the screening result is correct), and let the calling code interpret the results.