How to load Multidimensional array values into vector? - c++

This is part of the code (header and the main part):
#include <iostream>
#include <sstream>
#include <string>
#include <gl\GL.h>
#include <gl\GLU.h>
#include <glut.h>
#include <RassHost.h>
#include <api\iomap.h>
#include <api\iotrans.h>
#include <api\cgeometry.h>
#include <vector>
using namespace std;
int main()
{
cout << "Enter IP: " << endl;
getline(cin, server_ip);
enum(KEY_L = 'A', KEY_R = 'D', KEY_RUN = 'WW', KEY_JUMP='SPACE');
typedef OBJECT_3D_SYS_TYPES_NUM OBJECT3D_RCN_TYPE;
OBJECT3D_RCN_TYPE _psyObjects[][] = getPsyhicsPartObjects();
vector<OBJECT3D_RCN_TYPE> _objects;
//I would like to load _psyObjects[][] into vector<OBJECT3D_RCN_TYPE> _objects;
Server::StartGame(Server::getIP(), 8888, "-r run", false);
system("pause");
return 0;
}
Is it possible to copy _psyObjects values into vector<OBJECT3D_RCN_TYPE>?
I want to control the multidimensional array with vector api, if it is possible.
Thanks!

You'll need to create a vector of vectors:
vector< vector<OBJECT3D_RCN_TYPE> > _objects;
Then just fill it like a normal vector.
I'd post more code, but you need to know the dimensions of the array, and I can't see those from the code.

You could also use a Boost::multi_array. It's api is like std::vector's, but possibly similar enough to meet your needs.

Related

C++ Debug Assertion Failed While Using Opencv

I am trying to compare two image and check whether they belongs to same person. This code giving Debug assertion failed Like here. I have checked, it can access to photos. Then what is the problem. People say that you are trying to access the something which is not actually exist. However, I could not find anything about it in my code.
Regards;
#include <iostream>
#include <string>
#include "opencv2\core\core.hpp"
#include "opencv2\core.hpp"
#include "opencv2\face.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\objdetect\objdetect.hpp"
#include "opencv2\opencv.hpp"
#include<direct.h>
#include "vector"
using namespace std;
using namespace cv;
using namespace cv::face;
int main() {
vector<Mat> img;
img[0]= imread("C://Users//Gökhan//Desktop//faces//faces442.jpg");
Mat img2 = imread("C://Users//Gökhan//Desktop//faces//faces448.jpg");
vector<int> label;
label[0] = 1;
Ptr<LBPHFaceRecognizer> model = LBPHFaceRecognizer::create();
model->train(img[0], label[0]);
int predictedLabel = model->predict(img2);
if (predictedLabel == 1) {
cout << "found";
}
}
You are trying to access elements of vectors that have no elements.
You have to allocate elements before accessing.
You can use the constructor with specifying the number of elements to allocate to allocate elements, for example. To do this, change the lines
vector<Mat> img;
vector<int> label;
to
vector<Mat> img(1);
vector<int> label(1);

All strings are unidentified

#include <iostream>
#include <time.h>
#include <string.h>
#include <stdio.h>
int main()
{
string msg;
printf("Enter the message that you wish to display as scroller: ");
getline(cin,msg);
msg=msg+". ";
int x=0;
while(1)
{
Scroll(msg);
wait(100);
system("cls");
x++;
}
cin.get();
return 0;
}
I Have this C code and all strings in the file say 'identifier "string" is undefined'. I tried including <string> instead of <string.h> but it didn't work. Why is it not working?
Add
using namespace std;
After includes (but before main). Or, better, use notion of:
std::string // instead of string
Update: I missed the point of this being C-question. I will leave this answer, but for the sake of formality, use it if you came from Google and you are working with C++.
This is C++ code, not C.
The compiler is probably getting confused because it cannot parse it, so then it finds C-like code and all identifiers do not exist.
The includes should be:
#include <iostream>
#include <ctime>
#include <string>
#include <cstdio>
You are also missing a:
using namespace std;
Plus the definitions for Scroll and wait etc.

Segmentation fault (core dumped) when using larger boost-matrices

If I run the code the error occurs: Segmentation fault (core dumped)
The problem does not exist if I decrease the dimensions of MatSamplesDimM in the header-file (e.g. from 300000 to 30000) or if I just declare Matrix1 and not Matrix2 in the main-file. The problem is independent of environment in which I run the code (eclipse or from terminal).
Do you have any ideas? Thank you very much for your help!
StrangeBehavior.cpp is my main-File:
// System includes
#include <iostream>
#include <fstream> //fstream: Stream class to both read and write from/to files; for output
// Boost includes
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/math/distributions/normal.hpp> //to create normal distribution
#include <vector> // stl vector header
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp> //accumulator for mean, variance...
#include <boost/accumulators/statistics/mean.hpp>
#include <boost/bind/bind.hpp>
#include <boost/numeric/ublas/vector_proxy.hpp> // for using row()...
#include <boost/numeric/ublas/matrix_proxy.hpp> // for using row()...
#include <boost/numeric/ublas/vector_expression.hpp> // for using inner_prod
#include <boost/numeric/ublas/matrix_expression.hpp> // for using inner_prod, element_div
#include <boost/math/special_functions/gamma.hpp> // for Gamma function
#include "ParameterDeclaration.hpp"
using namespace std; // saves us typing std:: before vector
int main() {
std::cout << "Starting " << std::endl;
MatSamplesDimM Matrix1;
MatSamplesDimM Matrix2;
std::cout << "Finishing" << std::endl;
return 0;
}
and ParameterDeclaration.hpp my header-File:
#ifndef parameter_
#define parameter_
#include <iostream>
namespace ublas = boost::numeric::ublas;
typedef ublas::bounded_matrix<double,300000,2> MatSamplesDimM;
#endif
Try using the heap instead of the stack. This means allocate the matrices using new.

Unable to tokenize a string and pass to a vector <string>

I am experimenting with CGI in C++. I know that there are libraries which handle basic stuff, but in order to know whats going on under the hood i have been trying to parse the stdin using string datatype ---> tokenize using '= and &' then push_back into a vector. at the latter step, i am receiving segmentation fault. given below is the program where i am using cin>> to obtain user input and so on ..
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
int main()
{
vector <string> stuff(0);
vector<string>::iterator it;
char* bufferchar;
string buffer;
char str[size];
cout<<"Content-type: text/html\n\n"
<<"<html>"
<<"<head>"
<<"<title>CGI SCRIPT</title>"
<<"</head>"
<<"<body>"
fgets(str,20,stdin); //20 is expect size of content from html form
puts(str);
cout<<"<p>Now to break this guy apart";
int x=0;
bufferchar = strtok(str,"&=");
buffer+=bufferchar;
stuff.push_back(buffer);
while(bufferchar!=NULL){
cout<<bufferchar<<'\n'<<'\n';
bufferchar=strtok(NULL,"&=");
buffer+=bufferchar;
stuff.push_back(buffer);
}
cout<<"<br>ok does the vector iterate ?";
for (it=stuff.begin();it!=stuff.end();++it){
cout<<*it;
cout<<"<br> ok man, next <br><br>";
}
cout<<"</body>";
cout<<"</html>";
}

How to declare vectors in C++?

I'm trying to use a vector of strings in my code instead of an array of strings but apparently I miss some detail in the declaration of the vector. Using the following code, I get this error: ‘vector’ was not declared in this scope
// Try to implement a vector of string elements
#include<iostream>
using namespace std;
int main() {
const int MAX_ITEMS = 10;
vector<string> my_vector(MAX_ITEMS);
return 0;
}
How should I correctly declare the vector?
You should add these includes:
#include <vector>
#include <string>
You have to include the header:
#include <vector>
#include <string>
You need:
#include <vector>