initializing vector of char * does crazy things to my computer - c++

I am trying to solve this puzzle: http://www.puzzleup.com/2015/puzzle/?13
to do that, I want to calculate all the possible codes and pass them in a vector
You will produce a set of 7-letter codes using the the letters A, B,
C, D, E, F and G
So I tried to create vector of char arrays. The code is shared below:
#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <cmath>
char letters[] = {'a','b','c','d','e','f','g'};
char * tempArr;
char * pastArr;
std::vector<char * > deneme;
char s1[] = {'a', 'b'};
char s2[] = {'c', 'd'};
void calculateAllPossibilities(int depth, int lastDepth)
{
//depth 1 den baĆ¾layacak
for( int i = 0; i < sizeof(letters); i++ )
{
//
if ( depth != 1 && depth != lastDepth )
{
//
tempArr = new char[depth];
for( int j = 0; j < depth-1; j++ )
{
//
*tempArr = pastArr[j];
tempArr++;
}
*tempArr = letters[i];
for( int x = 0; x < depth; x++ )
{
//
std::cout << tempArr[x] << ",";
}
std::cout << std::endl;
delete pastArr;
pastArr = new char[depth];
for( int k = 0; k < depth; k++ )
{
//
*pastArr = tempArr[k];
pastArr++;
}
delete tempArr;
calculateAllPossibilities(depth + 1, lastDepth );
}
else if( depth == lastDepth )
{
//
tempArr = new char[depth];
for( int k = 0; k < depth - 1; k++ )
{
//
*tempArr = pastArr[k];
tempArr++;
}
*tempArr = letters[i];
for( int x = 0; x < depth; x++ )
{
//
std::cout << tempArr[x] << ",";
}
std::cout << std::endl;
deneme.push_back(tempArr);
delete tempArr;
}
else if( depth == 1 )
{
//
pastArr = new char[depth];
*pastArr = letters[i];
std::cout << pastArr[0] << std::endl;
delete tempArr;
calculateAllPossibilities(depth + 1, lastDepth );
}
}
}
int main()
{
calculateAllPossibilities(1,7);
std::cout << deneme[0][2];
return 0;
}
The problem is, when I try to cout the values of deneme vector without using cout in functions, it gives me different thing in each compilation. Like those: ":", "_", "X", "2" :)
However, then I added couts to function and tried to see whats going on and BAM! My computer goes crazy after couts!
What am I doing wrong here? First time I encountered something like this.

This is not going to work, at all
deneme.push_back(tempArr);
delete tempArr;
You save a pointer to an array in the vector, and then you delete the array. Now the saved pointer points nowhere.
It would work much better with a vector of std::string.
Also, when you allocate an array with tempArr = new char[depth];, the [] should also be used when deleting, like delete[] tempArray. That lets the compiler know that you are deleting an array, and not just one char. Not that it matters too much here.

Related

pushing strings into multidimensional vector c++

I am currently trying to separate a string by the number 2 and pushing the sub characters that i get into a 2d vector, the problem is that every time I try, I get a segmentation fault when I try to push the second row of characters in the vector. After trying a few things, I think the problem lies in the vector "some" after I clear the content of the vector. It seems to me that after the clearing, I am no longer able to push values into the vector. I hope that somebody has any suggestions because I am stuck.
std::string str = "11121112111";
std::vector<int> some;
std::vector<std::vector<int> > somemore;
for (unsigned int i = 0; i < str.length(); i++)
{
if (str[i] == '2')
{
somemore.push_back(some);
some.clear();
}
else
{
some.push_back(1);
}
}
for (unsigned int i = 0; i <= 3; i++)
{
for (unsigned int j = 0; j <= 3; j++)
{
std::cout << somemore[i][j] << std::endl;
}
}
With c++11 and Boost you can make a much more elegant solution, without the need for loops with an incrementing index.
#include <vector>
#include <string>
#include <iostream>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
int main()
{
std::string str = "11121112111";
std::vector<std::string> string_vector;
boost::split(string_vector, str, boost::is_any_of("2"));
std::vector<std::vector<int>> int_vector;
for (auto& s : string_vector)
int_vector.push_back(std::vector<int>(s.size(), 1));
for (auto& v : int_vector)
for (auto& i : v)
std::cout << i << std::endl;
return 0;
}
I would change the last part:
for(unsigned int i = 0; i <=3; i++)
{
for(unsigned int j = 0; j <=3; j++)
{
std::cout << somemore[i][j] << std::endl;
}
}
Into this:
for(unsigned int i = 0; i < somemore.size(); i++)
{
for(unsigned int j = 0; j < some.size(); j++)
{
std::cout << somemore[i][j] << std::endl;
}
}
It is much safer.
As I already mentioned in comments, you have two problems in your code:
You are not pushing the last some into somemore because there is no 2 at the end of str.
Your last loops are too large - You have a 3x3 matrix but you expect a 4x4 since you go from 0 to 3.
By the way, since you are only counting ones, you don't need some:
std::string str = "11121112111";
std::vector<std::vector<int>> somemore;
size_t count = 0;
for (size_t = 0; i < str.length(); i++) {
if (str[i] == '2') {
somemore.push_back(std::vector<int>(count, 1));
count = 0;
}
else {
++count;
}
}
for (size_t i = 0; i < somemore.size(); ++i) {
for (size_t j = 0; j < somemore[i].size(); ++j) {
std::cout << somemore[i][j] << std::endl;
}
}
You could also replace the last two loops with iterators, or if you have c++11 available:
for (const auto &s: somemore) {
for (const auto &v: s) {
std::cout << v << std::endl;
}
}
In this loop
for(unsigned int i = 0; i < str.length(); i++)
{
if(str[i] == '2')
{
sommore.push_back(som);
som.clear();
}
else
{
som.push_back(1);
}
}
where it is not clear whether som is the vector declared like
std::vector<int> some;
^^^^^
the last part of the string
std::string str = "11121112111";
^^^
is ignored by vector sommore. So the vector contains only two elements that corresponds to two 2(s) in the string.
As result these loops
for(unsigned int i = 0; i <=3; i++)
{
for(unsigned int j = 0; j <=3; j++)
{
std::cout << sommore[i][j] << std::endl;
}
}
that use the magic number 3 have undefined behaviour.
Even if the vector and its sub-vectors contain 3 elements even in this case conditions i <=3 and j <=3 are wrong.
You can adopt the following approach shown in this demonstratrive program
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<std::vector<int>> v;
std::string str = "11121112111";
for ( std::string::size_type pos = 0; ( pos = str.find_first_not_of( "2", pos ) ) != std::string::npos; )
{
auto n = str.find( "2", pos );
if ( n == std::string::npos ) n = str.size();
v.push_back( std::vector<int>( n - pos, 1 ) );
pos = n;
}
for ( const auto &row : v )
{
for ( int x : row ) std::cout << x;
std::cout << std::endl;
}
}
The program output is
111
111
111

String array with Core Dump (using templates and operator overloading)

I am trying to understand something about string arrays and core dumps. So, I created a class template with a few operator overloaded functions that add and remove elements. When I tested the addition and subtraction overloaded methods on int, double, and char specializations, the program added and removed the elements perfectly (no core dump); however, when trying this on strings I get a core dump. I made sure to have #include <string> and using std::string. Can anyone explain why this works on int, double, and char specializations, but not on strings?
template <class T> T Set<T>::iVal;
template <class T> T Set<T>::DELIM;
template <class T>
//
//default constructor
//
Set<T>::Set ( int s ){
psize = ( s > 0 ? s: DEFAULTSIZE);
//allocate an array of specified size
set = new T[ psize ];
if(!set) {
//send an error is system cannot allocate memory
cout << "Cannot Allocate Memory, exiting program... " << endl;
exit (1);
}
for ( int i = 0; i < psize; i++){
set[i] = iVal;
}
numOfElements = 0;
}
//
//custom constructor
//
template <class T>
Set<T>::Set( T array[] , int size, char name ){
set = new T[size];
psize = ( size > 0 ? size: DEFAULTSIZE);
numOfElements = size;
for ( int i = 0; i < psize; i++){
set[i] = array[i];
}
if (!set){
cout << "Cannot Allocate Memory, exiting program... " << endl;
exit(1);
}
Set::name = name;
}
//
//subtraction operator that removes elements
//
template <class T>
bool Set<T>::operator-( T n ){
bool status = false;
for ( int i = 0; i < numOfElements; i++){
if ( set [i] == n ){
for ( int j = i; j < numOfElements; j++){
set [j] = set[j + 1];
}
set[numOfElements - 1] = n;
--numOfElements;
}
status = true;
}
return (status);
}
//
//addition operator that adds elements
//
template <class T>
bool Set<T>::operator+( T n ){
bool status = false;
if ( !element(n) ){
if ( psize == numOfElements ) {
T *ptr = new T[psize += 1];
for ( int i = 0; i < numOfElements; i++ )
ptr[i] = set[i];
delete [] set;
set = ptr;
psize += 1;
delete [] ptr;
}
set[numOfElements] = n;
numOfElements += 1;
status = true;
}
return status;
}
//In Driver
//
//Creating string arrays using the custom constructor to instantiate objects
//
string s1[4] = { "turtle" , "fish", "shark" , "lobster" };
string s2[3] = { "turtle", "book", "pencil" };
//
//Testing string specialization
//
cout << "\nTesting strings.\n" << endl;
Set <string> string1 ( s1, 4, 'A' );
Set <string> string2 ( s2, 3, 'B' );
//setting delimiter
Set <string>::setDELIM ("stop");
//
//testing the addition and subtraction of elements
//CORE DUMP HAPPENS HERE
string1 - "shark";
cout << "\nAfter removing an element from Set A, Set A = " << string1 << endl;
string1 + "dolphin";
cout << "\nAfter adding an element to Set A, Set A = " << string1 << endl;
I see a couple of problems that are both undefined behaviour. The fact that it works with other datatypes may just be due to luck.
First, let's look at your - method:
if ( set [i] == n ){
for ( int j = i; j < numOfElements; j++){
set [j] = set[j + 1]; // <-- Access past last element in array
}
set[numOfElements - 1] = n;
--numOfElements;
}
That inner loop above should test j < numOfElements - 1. That way, set[j+1] will not look at the element past the end of the array (if numOfElements happens to be the same as the allocated size).
Now let's look at your + method:
T *ptr = new T[psize += 1];
for ( int i = 0; i < numOfElements; i++ )
ptr[i] = set[i];
delete [] set;
set = ptr;
psize += 1;
delete [] ptr; // <-- BOOM!
In the above, you just deleted set, then replaced it with new memory pointed to by ptr. But then you deleted that memory. If you remove the marked line, this should work.

Generate the Cartesian Product of 2 vector<string>s In-Place?

If I want to get the Cartesian Product of these two vector<string>s:
vector<string> final{"a","b","c"};
vector<string> temp{"1","2"};
But I want to put the result in final, such that final would contain:
a1
a2
b1
b2
c1
c2
I'd like to do this without creating a temporary array. Is it possible to do this? If it matters, the order of final is not important.
You may try the following approach
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector<std::string> final{ "a", "b", "c" };
std::vector<std::string> temp{ "1", "2" };
auto n = final.size();
final.resize( final.size() * temp.size() );
for ( auto i = n, j = final.size(); i != 0; --i )
{
for ( auto it = temp.rbegin(); it != temp.rend(); ++it )
{
final[--j] = final[i-1] + *it;
}
}
for ( const auto &s : final ) std::cout << s << ' ';
std::cout << std::endl;
return 0;
}
The program output is
a1 a2 b1 b2 c1 c2
Try the function cartesian:
#include <vector>
#include <string>
using namespace std;
void cartesian(vector<string>& f, vector<string> &o) {
int oldfsize = f.size();
f.resize(oldfsize * o.size());
for (int i = o.size() - 1; i>=0; i--) {
for (int j = 0; j < oldfsize; j++) {
f[i*oldfsize + j] = f[j] + o[i];
}
}
}
int main()
{
vector<string> f{"a","b","c"};
vector<string> temp{"1","2"};
cartesian(f, temp);
for (auto &s: f) {
printf("%s\n", s.c_str());
}
}
This works for me:
void testCartesianString(vector<string>& final,
vector<string>const& temp)
{
size_t size1 = final.size();
size_t size2 = temp.size();
// Step 1.
// Transform final to : {"a","a","b","b","c","c"}
final.resize(size1*size2);
for ( size_t i = size1; i > 0; --i )
{
for ( size_t j = (i-1)*size2; j < i*size2; ++j )
{
final[j] = final[i-1];
}
}
// Step 2.
// Now fix the values and
// change final to : {"a1","a2","b1","b2","c1","c2"}
for ( size_t i = 0; i < size1; ++i )
{
for ( size_t j = 0; j < size2; ++j )
{
final[i*size2+j] = final[i*size2+j] + temp[j];
cout << final[i*size2+j] << " ";
}
cout << endl;
}
}
This is just a personal preference option to Vald from Moscow's solution. I think it may be faster for dynamic arrays because there would be less branching. But I haven't gotten around to writing a timing test bench.
Given the inputs vector<string> final and vector<string> temp:
const auto size = testValues1.first.size();
testValues1.first.resize(size * testValues1.second.size());
for (int i = testValues1.first.size() - 1; i >= 0; --i){
testValues1.first[i] = testValues1.first[i % size] + testValues1.second[i / size];
}
EDIT:
Nope, this solution is slower not faster: http://ideone.com/e.js/kVIttT
And usually significantly faster, though I don't know why...
In any case, prefer Vlad from Moscow's answer

Removing zero rows from a Matrix (elegant way)

I have a matrix with some zero rows. I would like to remove the zero rows. The matrix is Nx3. What I've done is simple. I create std::vector in which every three elements represent a row then I convert it to Eigen::MatrixXd. Is there an elegant way to remove the zero rows?
#include <iostream>
#include <vector>
#include <Eigen/Dense>
Eigen::MatrixXd VecToMat(const std::vector<double> vec)
{
int rows(vec.size()/3) , cols(3);
Eigen::MatrixXd temp( rows , cols);
int count(0);
for ( int i(0); i < rows; ++i)
{
temp(i,0) = vec[count];
temp(i,1) = vec[count+1];
temp(i,2) = vec[count+2];
count += 3;
}
return temp;
}
Eigen::MatrixXd getNewMat(Eigen::MatrixXd& Z)
{
std::vector<double> vec;
for ( int i(0); i < Z.rows(); ++i)
{
if ( (Z(i,0) && Z(i,1) && Z(i,2)) != 0 ){
vec.push_back(Z(i,0));
vec.push_back(Z(i,1));
vec.push_back(Z(i,2));
}
}
Eigen::MatrixXd temp = VecToMat(vec);
return temp;
}
int main()
{
Eigen::MatrixXd Z(5,3);
Z.setOnes();
Z(0,0) = 0;
Z(0,1) = 0;
Z(0,2) = 0;
Z(1,0) = 0;
Z(1,1) = 0;
Z(1,2) = 0;
Z(2,0) = 0;
Z(2,1) = 0;
Z(2,2) = 0;
std::cout << Z << std::endl << std::endl;
std::cout << getNewMat(Z) << std::endl;
std::cin.get();
return 0;
}
Here is a full implementation I find quite elegant. Note that this one does not preserve order of non-zero rules, which maybe isn't what you want, but is more efficient both in complexity and lines of code:
void removeZeroRows(Eigen::MatrixXd& mat)
{
Matrix<bool, Dynamic, 1> empty = (mat.array() == 0).rowwise().all();
size_t last = mat.rows() - 1;
for (size_t i = 0; i < last + 1;)
{
if (empty(i))
{
mat.row(i).swap(mat.row(last));
empty.segment<1>(i).swap(empty.segment<1>(last));
--last;
}
else
++i;
}
mat.conservativeResize(last + 1, mat.cols());
}
Basically you can follow a pseudocode like this:
get N = rows, M = columns
iterate by each N
if N[0] = 0 iterate on rows exiting on first non-zero
if N[0] = 0 && .. && N[M] = 0
remove row
For removing single row:
void removeRow(Eigen::MatrixXd& matrix, unsigned int rowToRemove) {
unsigned int numRows = matrix.rows() - 1;
unsigned int numCols = matrix.cols();
unsigned int rowPos = numRows - rowToRemove;
if( rowToRemove < numRows ) {
matrix.block(rowToRemove, 0, rowPos, numCols) = matrix.block(rowToRemove + 1, 0, rowPos,numCols);
}
matrix.conservativeResize(numRows, numCols);
}
Save each nonempty row into a vector, then make a new matrix
vector<block> buffer; //not sure of the type name for the rows
VectorXd zero(3); //or appropriate comparable type
for(int i = 0; i < Z.rows(); i++){ //note: possibly a function call each time
if(Z.row(i) != zero)
//if((Z.row(i) != 0).any()) //broadcasting comparison?
buffer.push_back(Z.row(i));
}
MatrixXd return_value(buffer.size(), 3);
for(int i = buffer.size(); i --> 0;)
return_value.row(i) = buffer[i];
return return_value;
Warning: Resizing the old one instead of making a new one may erase the contents before you can save them.
I can't read the docs from here, so you will have to see for yourself what kind of comparison operations you can do on block objects. As a last result, try row.any() (faster?) or row.squaredNorm() == 0.

Swapping Pointers of Array

#include <iostream>
#include <cstdlib>
using namespace std;
void swapNum(int *q, int *p)
{
int temp;
temp = *q;
*q = *p;
*p = temp;
}
void reverse(int *ip, int const size)
{
for (int k = 0; k < size; k++)
{
if (k == (size/2))
{
int *q = &ip[k];
int *p = &ip[k+1];
swapNum(q,p);
break;
}
else
swap(ip[k], ip[size-k]);
}
}
int main()
{
const int size = 20;
int arr[size];
int *ip;
ip = arr;
cout << "Please enter 20 different numbers." << endl;
for (int i = 0; i < size; i++)
{
cout << "\nNumber " << i+1 << " = ";
cin >> ip[i];
}
reverse(ip, size);
cout << "I will now print out the numbers in reverse order." << endl;
for (int j = 0; j < size; j++)
{
cout << ip[j] << " ";
}
return 0;
}
When I try to run this program it crashes. I don't know what's wrong and the purpose of my program is to swap number of the array using pointers. I am recently introduced to this so I am not that familiar with it. But I think that I am swapping the address of the numbers instead of swapping the numbers in the address. Correct me if I am wrong.
You're accessing outside the array bounds in reverse() when you do:
swap(ip[k], ip[size-k]);
On the first iteration of the for loop, k is 0 and size-k is size. But array indexes run from 0 to size-1. So it should be:
swap(ip[k], ip[size-k-1]);
But I don't see a definition of swap in your program. I think it should actually be:
swapNum(&ip[k], &ip[size-k-1]);
Another improvement: Instead of handling size == k/2 specially and using break, just use size < k/2 as the bound test in the for loop.
swap(ip[k], ip[size-k]);
Your problem is there. size - k when k is 0 will lead to undefined behavior (accessing an array out of bounds). Your loop structure in reverse can be simplified:
for (int k = 0; k < size / 2; k++)
swapNum(&ip[k], &ip[size - k - 1]); // updated to use the address since your swap function takes pointers.
Function reverse is invalid
void reverse(int *ip, int const size)
{
for (int k = 0; k < size; k++)
{
if (k == (size/2))
{
int *q = &ip[k];
int *p = &ip[k+1];
swapNum(q,p);
break;
}
else
swap(ip[k], ip[size-k]);
}
}
For example when k is equal to 0 then you call
swap(ip[0], ip[size]);
However the array has no element with index size.
ALso you mess two functions std::swap and swapNum
This code snippet also is invalid
if (k == (size/2))
{
int *q = &ip[k];
int *p = &ip[k+1];
swapNum(q,p);
break;
}
When size is an even number (or an odd number) as in your code then you make incorrect swap. For example if size is equal to 20 then you should swap ip[9[ with ip[10]. However according to the code snippet above you swap ip[10] with ip[11].
You could use standard algorithm std::reverse
for example
#include <algorithm>
#include <iterator>
//...
std::reverse( std::begin( arr ), std::end( arr ) );
or
#include <algorithm>
//...
std::reverse( arr, arr + size );
If you want to write the function yourself then it could look as
void reverse( int a[], int size )
{
for (int k = 0; k < size / 2; k++)
{
int tmp = a[k];
a[k] = a[size-k-1];
a[size-k-1] = tmp;
}
}
Or if you want to use your function swapNum then
void reverse( int a[], int size )
{
for (int k = 0; k < size / 2; k++)
{
swapNum( &a[k], &a[size-k-1] );
}
}
EDIT: I removed qualifier const from the first parameter that was a typo.