Setting vector<double> as choice values of wxComboBox - c++

I am creating my first C++ program with GUI.
I am using Code::Blocks v16.01 and wxWidgets v3.03.
I have found that in the wxComboBox class constructor, the type that stands for choices is wxArrayString. I have tried to convert vector to vector and later to vector and to wxArrayString, but it failed miserably...
My question is how to set default choice values of wxComboBox object?
Preferably I would like them to be filled with values of vector, created during program execution.
Here is the code:
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
vector <double> vector_double;
vector <string> vector_string;
vector <wxString> vector_wxstring;
void convert_double_to_string(vector<double> &dbl, vector <string> &str)
{
for (int i = 0; i < dbl.size(); i++)
{
ostringstream stream;
stream << dbl[i];
str.push_back(stream.str());
}
}
void convert_string_to_wxString(vector<string> & str, vector <wxString> &wxstr);
{
for (int i = 0; i < str.size(); i++)
{
wxstr.push_back(_(str[i]));
}
}
void main()
{
/////////
// here setting vector_double's values
//////////
convert_double_to_string(vector_double, vector_string);
convert_string_to_wxString(vector<string> vector_string, vector_wxstring);
}
This is what I have got. The string conversion to wxString is not working though. And even if it would, I wouldn't know how to insert it into wxArrayString.

Something along these lines:
wxComboBox * cbo = new wxComboBox( ... );
wxArrayString as;
for ( auto& s : vector_string )
as.Add( s );
cbo->Set( as );

Related

returning a vector in get/set method

I am trying to use a get/set method in c++ for a vector. I keep getting an error throwing an instance of std::out_of_range which occurs when i try to cout the index value of ego_points.at(1) in my main code. I am providing my header file and my main code below but I'll try to explain here:
I have a class called Points with a public function called ScanCallback that builds the vector scannedData and uses the set function to set the private vector transmittedData equal to the scannedData. In my main code, I have an object called p1 of the Points class and I am trying to set a vector called ego_points equal to the transmittedData of object p1. I am able to build the vector ego_points by using the getVector() function but cannot access it's data. Anyone know why not?
Header File:
#pragma once
#include "ros/ros.h"
#include "sensor_msgs/LaserScan.h"
#include "iostream"
#include "string"
#include "motor_driver/Motor_speeds.h"
#include "motor_driver/cartesian.h"
#include <vector>
#ifndef MOTOR_DRIVER_H
#define MOTOR_DRIVER_H
using namespace std;
using namespace motor_driver;
class Points {
public:
vector<float> scannedData;
int i;
int size;
int size1;
int size2;
Points() : scannedData(0) {} //Must match the class name. This is the constructor.
void set(vector<float> transmittingData){
transmittedData = transmittingData;
}
void ScanCallback(const sensor_msgs::LaserScan::ConstPtr& scan) {
scannedData.clear();
size = scan->ranges.size();
scannedData.resize(size);
for(i = 0; i < size; i = i + 1){
scannedData.at(i) = scan->ranges[i];
}
set(scannedData);
}
int getSize(){
return size;
}
vector<float> getVector(){
return transmittedData;
}
private:
vector<float> transmittedData;
};
#endif
Main Code:
#include "ros/ros.h"
#include "sensor_msgs/LaserScan.h"
#include "iostream"
#include "string"
#include "motor_driver/Motor_speeds.h"
#include "motor_driver/cartesian.h"
#include <motor_driver.h>
#include <vector>
using namespace std;
using namespace motor_driver;
Points p1;
int main(int argc, char** argv) {
ros::init(argc, argv, "motor_driver_node");
ros::NodeHandle nh;
ros::Subscriber sub;
ros::Rate r(1);
int ss;
int newsize;
int index;
vector<float> ego_points;
while (ros::ok()) {
sub = nh.subscribe<sensor_msgs::LaserScan>("/scan",10, &Points::ScanCallback, &p1);
newsize = p1.getSize();
ego_points.clear();
ego_points.resize(newsize);
ego_points = p1.getVector();
cout << ego_points.at(1) << endl;
r.sleep();
ros::spinOnce();
}
return 0;
}
My error:
:~/Desktop/Naes_Thesis$ rosrun motor_driver motor_driver_node
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check
Aborted (core dumped)
does the vector have at least two elements? If you want to access the first element you should use:
cout << ego_points.at(0) << endl;
Also the size method is probably incorrect. It should be something like this:
int getSize() const {
return transmittedData.size();
}
Otherwise the size may contain a different value than the actual vector size
Actually I figured it out. The issue was that the size of the vector is 0 until the vector is built. So I added a if (vector =0) then (do nothing) portion to the code and it fixed it. Thank you Noel-lopes and 1201ProgramAlarm for your contribution.

What is the easiest way to initialize a 2d array from a vector of strings?

I have:
vector<string> myVector = {0};
myVector.push_back("first");
myVector.push_back("second");
char *list[] = ????
I want it to be initialized like if I was doing this
char *list[] = { "first", "second", NULL };
I know I can start allocating memory based on the size and of the vector and the size of the longest string in the vector (list[v.size()+1][longest_string_in_vector]) but I wanted to see I'm not thinking of something that might be easier/faster.
If the legacy code requires a char **, then to create a variable list, you can create a vector as you initially are doing in your question.
After that, create a std::vector<char *>, where the pointers are pointers within the vector for each item. Of course, you have to ensure that the vector doesn't go out of scope or is resized. It has to be fully "set up" before creating the std::vector<char *>.
In addition, since you are certain that the legacy function does not attempt to alter the strings sent to it, we should take away the "constness" of the strings.
#include <vector>
#include <string>
#include <iostream>
void legacy_function(char **myList)
{
for (int i = 0; myList[i]; ++i)
std::cout << myList[i] << "\n";
}
using namespace std;
int main()
{
vector<string> myVector;
myVector.push_back("first");
myVector.push_back("second");
//...
// create the pointer vector
vector<char *> myPtrVector;
// add pointer to string to vector
for (size_t i = 0; i < myVector.size(); ++i)
myPtrVector.push_back(const_cast<char*>(myVector[i].c_str()));
// stick the null at the end
myPtrVector.push_back(NULL);
// ...
// call legacy function
legacy_function(&myPtrVector[0]);
}
Basically, we created the strings in a vector, and created another vector that stores pointers to the strings.
Note that the function legacy_function takes a char **, and all we need to do is pass it the address of the first element in our pointer vector.
Live Example: http://ideone.com/77oNns
Edit: Rather than having the code strewn in different areas of your program, a better approach in terms of code organization is to encapsulate the creation of the array:
#include <vector>
#include <string>
class CharPtrPtr
{
std::vector<std::string> m_args;
std::vector<char *> m_argsptr;
public:
void add(const std::string& s) { m_args.push_back(s); }
char ** create_argsPtr()
{
m_argsptr.clear();
for (size_t i = 0; i < m_args.size(); ++i)
m_argsptr.push_back(const_cast<char*>(m_args[i].c_str()));
m_argsptr.push_back(NULL);
return &m_argsptr[0];
}
char **get_argsPtr() { return m_argsptr.empty()?NULL:&m_argsptr[0]; }
void clear_args() { m_args.clear(); m_argsptr.clear(); }
};
#include <iostream>
void legacy_function(char **myList)
{
for (int i = 0; myList[i]; ++i)
std::cout << myList[i] << "\n";
}
int main()
{
CharPtrPtr args;
args.add("first");
args.add("second");
legacy_function(args.create_argsPtr());
}
Live Example: http://coliru.stacked-crooked.com/a/834afa665f054a1f
I tried these two ways,
1.Initialize manually
char *list[] = { (char*)&myVector[0][0], (char*)&myVector[1][0] };
2.Initialize in a loop
char **list2 = new char*[ myVector.size() ];
for ( unsigned int i = 0; i < myVector.size(); ++i ) {
list2[ i ] = (char*)&myVector[ i ][0];
}
However these lists only have pointers to the each string in the vector and don't actually have a copy. If you change the strings, you'll see the changes from the lists. But if you empty the vector then the lists will have a dangling pointer.
3.If you want a copy of the strings then,
char **list = new char*[ myVector.size() ];
for ( unsigned int i = 0; i < myVector.size(); ++i ) {
list[ i ] = new char[myVector[i].size()+1];
strcpy( list[ i ], &myVector[i][0] );
}
I wouldn't write this code but, there you go..

c++ segmentation fault for dynamic arrays

I want to add a theater object into a boxoffice object in a C++ code. When I try to add it in main code, first one is added successfully. But a segmentation fault occurs for second and obvioulsy other theater objects. Here is the add function;
#include <iostream>
#include <string>
#include "BoxOffice.h"
using namespace std;
BoxOffice::BoxOffice()
{
sizeReserv = 0;
sizeTheater = 0;
theaters = new Theater[sizeTheater];
reserv = new Reservation[sizeReserv];
}
BoxOffice::~BoxOffice(){}
void BoxOffice::addTheater(int theaterId, string movieName, int numRows, int numSeatsPerRow){
bool theaterExist = false;
for(int i=0; i<sizeTheater; i++)
{
if(theaters[i].id == theaterId)
{
theaterExist=true;
}
}
if(theaterExist)
cout<<"Theater "<<theaterId<<"("<<movieName<<") already exists"<< endl;
else
{
++sizeTheater;
Theater *tempTheater = new Theater[sizeTheater];
if((sizeTheater > 1)){
tempTheater = theaters;
}
tempTheater[sizeTheater-1] = Theater(theaterId,movieName,numRows,numSeatsPerRow);
delete[] theaters;
theaters = tempTheater;
cout<<"Theater "<<theaterId<<"("<<movieName<<") has been added"<< endl;
cout<<endl;
delete[] tempTheater;
}
}
And I get segmentation fault on this line;
tempTheater[sizeTheater-1] = Theater(theaterId,movieName,numRows,numSeatsPerRow);
This is Theater cpp;
#include "Theater.h"
using namespace std;
Theater::Theater(){
id=0;
movieName="";
numRows=0;
numSeatsPerRow=0;
}
Theater::Theater(int TheaterId, string TheaterMovieName, int TheaterNumOfRows, int TheaterNumSeatsPerRow)
{
id = TheaterId;
movieName = TheaterMovieName;
numRows = TheaterNumOfRows;
numSeatsPerRow = TheaterNumSeatsPerRow;
theaterArray = new int*[TheaterNumOfRows];
for(int i=0;i<TheaterNumOfRows;i++)
theaterArray[i]= new int[TheaterNumSeatsPerRow];
for(int i=0; i<TheaterNumOfRows;i++){
for(int j=0;j<TheaterNumSeatsPerRow;j++){
theaterArray[i][j]=0;
}
}
}
This is header file of Theater;
#include <iostream>
#include <string>
using namespace std;
class Theater{
public:
int id;
string movieName;
int numRows;
int numSeatsPerRow;
int **theaterArray;
Theater();
Theater(int TheaterId, string TheaterMovieName, int TheaterNumOfRows, int TheaterNumSeatsPerRow);
};
And this is how i call add functions;
BoxOffice R;
R.addTheater(10425, "Ted", 4, 3);
R.addTheater(8234, "Cloud Atlas", 8, 3);
R.addTheater(9176, "Hope Springs",6,2);
The problematic lines are these:
if((sizeTheater > 1)){
tempTheater = theaters;
}
First you allocate memory and assign it to tempTheater, but here you overwrite that pointer so it will point to the old memory. It does not copy the memory. Since the code is for a homework assignment, I'll leave it up to you how to copy the data, but I do hope you follow the rule of three for the Theater class (as for the BoxOffice class) which will make it very simple.
Also, there's no need to allocate a zero-size "array", just make the pointers be nullptr (or 0).

Create a struct in each iteration

I want to store queues in a queue structure from the stl library. For some reasons I have to store a queue in each iteration of my program, but I think that create a new queue over and over again is too expensive.
I only know two ways to do this. The first one:
#include <iostream>
#include <deque>
using namespace std;
int main () {
unsigned int limit, i = 0;
deque<deque<int> > container;
cin >> limit;
for ( ; i < limit; i++ ) {
deque<int> values;
//set some values in the values structure.
setValues(values, i);
container.push(values);
}
}
the second one:
#include <iostream>
#include <deque>
using namespace std;
int main () {
unsigned int limit, i = 0;
deque<deque<int> > container;
deque<int> values;
cin >> limit;
for ( ; i < limit; i++ ) {
//reset the structure, i.e. delete al the previous values.
reset(values);
//set some values in the values structure.
setValues(values, i);
container.push(values);
}
}
the problem here is that I don't know any function for reset my queue, or maybe I have to do values=NULL?
How can I do this in an efficient way?
Thanks! :D
You can push an empty deque in the loop, get a reference to it, and then add items to it.
#include <iostream>
#include <deque>
using namespace std;
int main () {
unsigned int limit, i = 0;
deque<deque<int> > container;
cin >> limit;
for ( ; i < limit; i++ ) {
container.push_back(deque<int>());
deque<int>& values = container.back();
//set some values in the values structure.
setValues(values, i); }
}
You should check in debugger what your compiler is actually doing when you make copies of deque. I have checked in VS2013 and its all move semantics - as expected. This is a test code:
std::deque<int> getValues() {
std::deque<int> res;
res.push_back(1);
return res; // deque(_Myt&& _Right) called, also other compilers might use RVO
}
std::deque<int> ff;
std::deque<std::deque<int>> aff;
aff.push_back(getValues()); // void push_back(value_type&& _Val) called
at first it looks like a lot of copying, but actually in both problematic places move semantics are used, and only pointers of temporary objects are copied, so its all super fast.
But maybe you are stuct in pre c++11 world? At least this fragment
deque<deque<int> > container;
^^^
gives such a hint.

C++ code pass compling but return Segmentation fault

I have the following C++ code for practising sequence list and it passed the complier. However, when I try to run it, it returns Segmentation fault. Please help!! Thanks a lot.
main.cpp
#include <iostream>
#include <string>
#include "SeqList.h"
using namespace std;
int main() {
SeqList seq;
string vv[] = {"a", "b", "c", "d"};
for (int i = 0; i< 4; i++) {
seq.addElement(vv[i], i);
}
string* v = seq.getSeq();
for (int i=0; i<seq.getSeqSize(); i++) {
cout << v[i] <<endl;
}
return 0;
}
SeqList.h
#include<iostream>
#include<string>
using namespace std;
class SeqList {
private:
string seq[];
int size;
public:
void addElement(string, int);
void delElement(string, int);
string* getSeq();
int getSeqSize();
};
SeqList.cpp
#include <iostream>
#include <string>
#include "SeqList.h"
using namespace std;
string seq[100];
int size = 0;
string* SeqList::getSeq(){
return seq;
};
int SeqList::getSeqSize(){
return size;
};
void SeqList::addElement(string str, int pos) {
int i;
for (i = size; i > pos; i--) {
seq[i] = seq[i-1];
}
seq[i-1] = str;
size++;
};
Your segfault is happening because you're trying to access seq[i-1] in addElement when i = 0. This tries to access the memory outside of seq which causes a segfault. Try using seq[i] and seq[i+1] instead of seq[i-1] and seq[i], though you'll have to make sure you never call that code with more than 99 values or you'll run into a similar problem where the program tries to access memory past the end of seq.
Also, in SeqList.cpp
string seq[100];
int size = 0;
These lines are creating new variables, when it looks like you're trying to change the values you made in SeqList.h. To change those private values in your class you should either use a constructor or other function to initialize the values.