How to emit a string with quotes using yaml-cpp? - yaml-cpp

I want to emit a string with quotes using yaml-cpp so it looks like
time_stamp: "August 10, 2011 01:37:52"
in the output yaml file. How do I do it? Thanks.

YAML::Emitter out;
// ...
out << YAML::DoubleQuoted << "August 10, 2011 01:37:52";

Related

Can't Emit an Empty Value with yaml-cpp

I would like to emit an empty value but when I assign an empty string to be emitted the output is not technically empty.
Code Snippet:
YAML::Emitter out;
std::string name;
out << YAML::Key << "name";
out << YAML::Value << name;
Expected yaml Output:
name:
Actual yaml Output:
name: ""
As you can see I have an empty string defined and I expect the yaml output to effectively be empty.
Is this intended behavior? If so is there a way to work around this? I'm aiming to have my entire yaml output be quote free.
The YAML
name:
doesn't have a string value for the key name; it's actually a null value. See, e.g., an online parser here; the canonical YAML representation is:
!!map {
? !!str "name"
: !!null "null",
}
yaml-cpp is trying to ensure that what you told it ("write this empty string") is how the resulting YAML will be parsed, so it's writing the empty string as "".
If you want to write a null value, then either don't write a value, or write YAML::Null. The latter (I believe) will produce
name: ~
which is the canonical form of null.

How to save/serialize QVariant that is QVector<int>

I'm lost as to how to fix this :
qRegisterMetaType<QVector<int>>("QVector<int>");
QMap<int, QVariant> wah;
wah[0] = QVariant::fromValue(QVector<int>{12, 231, 45, 125, 123, 12, 312, 4, 12});
qDebug() << wah;
QByteArray ar;
QDataStream s(&ar, QIODevice::WriteOnly);
s << wah;
Any ideas/help would be great. Most of the google results are about serializing custom classes and not ints :/
TIA!
Needed to add
qRegisterMetaTypeStreamOperators<QVector<int>>("QVector<int>");
Bit of a bummer that this is not explained in docs tho.

Get int representation NTL GF2E

How I will be able to get the int representation in Galois Field using NTL library. For example of element
GF2E xx=random_GF2E();
I'm trying use
printf("%d",xx._GF2E__rep.HexOutput);
but I get 0
GF2E is an extension field, i.e. the elements in GF2E are living in GF(2)[X]/(P), where P is an irreducable polynomial. So you can not get an integer representation. But you can get a representation as a vector.
GF2X P;
SetCoeff(P, 0, 1);
SetCoeff(P, 1, 1);
SetCoeff(P, 2, 1);
// P is now x^2+x+1, this is irreducable since P(1)=1 and P(0)=1
GF2E::init(P);
GF2E xx = random_GF2E();
cout << xx << endl; // Prints something like "[0 1]"
Notice: To use this code snippet, you have to import the namespaces NTL and std to your programm by using using namespace NTL; and using namespace std; after your includes.
An other way would be to add NTL:: to all the NTL functions and std:: to cout and endl.
See this tutorial for more information about namespaces.

writing output to a file in Graphchi

I wrote a shortest path code in Graphchi and I wanted to print the output of that in a file. I was trying to use the template shown in the examples but I get error if I use the sameway of writing to a file as in other examples.
I have got stuck here. As the output I just want to print (vertex id,its minimum distance from source).
How can i do that.
Here is example how you can output values of all vertices to the console. It is easy to modify it to write the output to a file. Note that if you can handle binary files, GraphChi already has the vertex values in a file: .B.vout, where is sizeof(VertexDataType).
1) You need to define a callback-function, which will take vertex id and value as parameter
class OutputVertexCallback : public VCallback<VertexDataType> {
public:
virtual void callback(vid_t vertex_id, VertexDataType &value) {
std::cout << vertex_id << "=" << value << std::endl;
}
};
2) Then you need to call foreach_vertices() as follows to get the output:
OutputVertexCallback callback;
foreach_vertices<VertexDataType>(filename, 0, engine.num_vertices(), callback);

YAML ofstream emitter

I find this example:
ofstream ofstr("output.yaml");
YAML::Emitter out(ofstr);
out << some_large_document;
// not necessary anymore:
// ofstr << out.c_str()
But when i try use it, i have:
D:\work\C\map.cpp||In function `int main()':|
D:\work\C\map.cpp|24|error: no matching function for call to `YAML::Emitter::Emitter(std::ofstream&)'|
D:\work\C\yaml-cpp\emitter.h|23|note: candidates are: YAML::Emitter::Emitter(YAML::Emitter&)|
D:\work\C\yaml-cpp\emitter.h|25|note: YAML::Emitter::Emitter()|
||=== Build finished: 1 errors, 0 warnings ===|
There's no constructor to YAML::Emitter that takes a stream. (Where did you find that example?)
Instead, you do need to use the commented out line:
ofstream ofstr("output.yaml");
YAML::Emitter out;
out << some_large_document;
ofstr << out.c_str(); // is necessary!