How to pass Boost UUID random generator output to main - c++

I am trying to learn how Boost UUID works. I have made the following code
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <iostream>
using namespace std;
using namespace boost;
using namespace uuids;
int main() {
for (int i = 0; i < 40; ++i){
uuid uuid = random_generator()();
cout <<i+1<<" "<<"\t"<<uuid << endl;
}
system("pause");
return 0;
}
In this code i generate 40 lines with UUID codes. I want to place these lines in a function and call them by my main class int main()
Any ideas how to do this?

Generate them into a standard library container, such as e.g. std::vector<uuid>:
std::vector<uuid> foo() {
std::vector<uuid> r;
std::generate_n(back_inserter(r), 40, random_generator());
return r;
}
Live On Coliru
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <iostream>
using namespace std;
using namespace boost;
using namespace uuids;
std::vector<uuid> foo() {
std::vector<uuid> r;
std::generate_n(back_inserter(r), 40, random_generator());
return r;
}
int main() {
for(auto& uuid : foo())
cout << uuid << endl;
}

Related

Why my .cc and .h could be compiled and run on my own test.cc but not on TA's code

I'm totally new as a C++ programmer. I met a weird problem.
Our professor asked us to create a mergestrings function and a header file. The code part is simple and works good on my own test code. However, it cannot be compiled on TA's code.
I found it's because I didn't add using namespace std; in my header file.
Here is the header file and function.cc file:
#ifndef mergeStrings_h
#define mergeStrings_h
string mergeStrings(const string &a, const string &b);
#endif
#include <iostream>
#include <string>
using namespace std;
#include "mergeStrings.h"
string mergeStrings(const string &a, const string &b){
int len1 = a.size(), len2 = b.size(), i=0, j=0;
string res = "";
while (i<len1 || j<len2){
if (i<len1)
res.push_back(a[i++]);
if (j<len2)
res.push_back(b[j++]);
}
return res;
};
It COULD be compiled and run on my own test code:
#include <iostream>
#include <string>
using namespace std;
#include "mergeStrings.h"
int main(){
string a = "ace1356789";
string b = "bdf24";
cout<<mergeStrings(a,b)<<endl;
return 0;
}
It COULD NOT be compiled on TA's code.
Here is part of it:
#include <iostream>
#include <string>
#include "mergeStrings.h"
using std::cout;
using std::endl;
using std::string;
const int points_per_test = 10;
void testTwoString(const string &test_name, const string &s1, const string &s2,...
if (mergeStrings(s1, s2) == expected_string) {
total_grade += points_for_this_test;
cout << test_name + " succeeded! +" << points_for_this_test << endl;
} else {
cout << test_name + " failed!" << endl;
}
}
int main() {...
I don't understand why it could be run on my own code.
Your cpp files have using namespace std before #include "mergeStrings.h", so std::string has been pulled into the global namespace.
Your best option is to not write using namespace std (see Why is "using namespace std;" considered bad practice?), but instead fully qualify your types and include the necessary header.
#ifndef mergeStrings_h
#define mergeStrings_h
#include <string>
std::string mergeStrings(const std::string &a, const std::string &b);
#endif
#include <iostream>
#include <string>
#include "mergeStrings.h"
std::string mergeStrings(const std::string &a, const std::string &b){
int len1 = a.size(), len2 = b.size(), i=0, j=0;
std::string res = "";
while (i<len1 || j<len2){
if (i<len1)
res.push_back(a[i++]);
if (j<len2)
res.push_back(b[j++]);
}
return res;
};
#include <iostream>
#include <string>
#include "mergeStrings.h"
int main(){
std::string a = "ace1356789";
std::string b = "bdf24";
cout<<mergeStrings(a,b)<<endl;
return 0;
}

Get size of variable length array in llvm

Consider I've the following code
void foo(int n){
int arr[n];
}
int main(){
foo(5);
}
is there anyway to get the value of n (in this case 5) for the array in the function foo
This is what I've come up with so far
#include <clang/CodeGen/CodeGenAction.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/CompilerInvocation.h>
#include <clang/Basic/DiagnosticOptions.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <llvm/ADT/IntrusiveRefCntPtr.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/Support/SourceMgr.h>
#include <llvm/IR/Instructions.h>
using namespace llvm;
int main(){
llvm::LLVMContext *llvmcx;
static llvm::LLVMContext MyGlobalContext;
llvmcx = &MyGlobalContext;
llvm::SMDiagnostic Err= llvm::SMDiagnostic();
std::unique_ptr<llvm::Module> module=llvm::getLazyIRFileModule("test.ll",Err,*llvmcx,false);
for (auto curFref = module->getFunctionList().begin(), endFref = module->getFunctionList().end();
curFref != endFref; ++curFref) {
for(auto bb=curFref->getBasicBlockList().begin();bb!=curFref->getBasicBlockList().end();bb++){
for(auto ii=bb->getInstList().begin();ii!=bb->getInstList().end();ii++){
if(llvm::dyn_cast<llvm::AllocaInst>(ii)){
AllocaInst *t=dyn_cast<AllocaInst>(ii);
if(t->isArrayAllocation()) {
if (llvm::ConstantInt* CI=dyn_cast<llvm::ConstantInt(t->getArraysize()){
// this if is not satisfied
}
}
}
}
}
}
}
Is there any other way to get the size of a variable sized array?

Segmentation fault on accessing vector class member

I have a segmentation fault on this line in c++ :
vector<TemplateElement*> children = getChildren();
Class That inherits from abstract class TemplateElement
How to fix that ?
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include "strings.h"
#include "aimlthat.h"
using namespace std;
using namespace aiml;
vector<string> That::elements() {
vector<TemplateElement*> children = getChildren();
vector<string> elements;
for (int i = 0, n = children.size(); i < n; i++) {
string text = children[i]->toString();
text = trim(text);
vector<string> vsText = split(text);
for(int j=0, m=vsText.size(); j<m; ++j) {
elements.push_back(vsText[j]);
}
}
return elements;
}
/****************************************************************************/
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <map>
#include "strings.h"
#include "aimltemplateelement.h"
#include "aimltext.h"
using namespace std;
using namespace aiml;
vector<TemplateElement*> TemplateElement::getChildren() {
return m_vtChildren;
}
/***************************************************************************/
#ifndef __AIMLTEMPLATEELEMENT_H__
#define __AIMLTEMPLATEELEMENT_H__
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <map>
#include "strings.h"
#include "aimlelement.h"
using namespace std;
using namespace aiml;
namespace aiml {
class TemplateElement : public AIMLElement {
public:
TemplateElement() {}
TemplateElement(vector<TemplateElement*> elements);
vector<TemplateElement*> getChildren();
virtual string toString() = 0;
private:
vector<TemplateElement*> m_vtChildren;
};
}
#endif
/***********************************************************************/
Here is the class That :
#ifndef __AIMLTHAT_H__
#define __AIMLTHAT_H__
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "aimltemplateelement.h"
using namespace std;
namespace aiml {
class That : public TemplateElement {
public:
That() {}
vector<string> elements();
private:
};
}
#endif
You need to check for null pointer:
for (int i = 0, n = children.size(); i < n; i++) {
if(children[i]) //Be sure that children[i] is not null before dereferencing it
{
string text = children[i]->toString();
text = trim(text);
vector<string> vsText = split(text);
for(int j=0, m=vsText.size(); j<m; ++j) {
elements.push_back(vsText[j]);
}
}
}
[EDIT]
From your comment I see that all you need is initialize the pointer to That before using it, and btw don't forget to delete it when you finish:
That* that = new That();
that->elements();

How to convert char* to string *?

Tell me how to create different strings in one pointer string like array.
see following two program. 1st one give an errors. what is wrong here?
Kindly correct it.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string *j={"nilesh",
"rohit",
"samir",};
cout<<j<<endl;
}
#include <stdio.h>
const int MAX = 4;
int main ()
{
char *names[] = {"Zara Ali","Hina Ali","Nuha Ali","Sara Ali",};
int i = 0;
for ( i = 0; i < MAX; i++)
{
printf("Value of names[%d] = %s\n", i, names[i] );
}
return 0;
}
Write simply
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s[] = { "nilesh", "rohit", "samir", };
for ( const string &t : s ) cout << t << endl;
}
Also instead of the array you could use standard class std::vector<std::string>
For example
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> v = { "nilesh", "rohit", "samir", };
for ( const std::string &s : v ) std::cout << s << std::endl;
}
Why not try it in this way ?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string j[]={"nilesh",
"rohit",
"samir"};
cout<<j<<endl;
}
Printing j directly wont print all the three names. You need to print j[0], j[1] ...

C++ segmentation fault on input

I am getting a segmentation fault error after inserting an input file in this program I can't find where is the problem, could anyone help me with this?(it's the solution for the USACO training milk2 question).
#include <algorithm>
#include <bitset>
#include <limits>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main ()
{
std::ifstream in ("milk2.in");
std::ofstream out ("milk2.out");
std::vector< pair <int,int> > v;
int n,i,maxn,maxs,t,ts;
in >> n;
for (i = 0; i < n; i++)
{
in >> v[i].first >> v[i].second;
}
for (i = 0; i < n; i++)
{
if (v[i].second<(v[i+1].first)
{
t=v[i+1].first-v[i].second;
if (t>maxn){
maxn=t;
}
}
else
{
ts=v[i+1].second-v[i].first;
if (ts>maxs)
{
maxs=ts;
}
}
}
out << maxs <<" "<< maxn;
return 0;
}
When you create the vector v, it's empty. So any index, even zero, will be illegal.
You need to first create the entries in the vector, e.g. by doing push_back.