I've been looking, but i've never been able to save anything other than one coordinate in a 2 dimensional array
This is called serialization. Have a look at this question for more info on using Boost to do just that.
Based on StackedCrooked's idea, here's a solution that allows you to user either a C-style array of std::vector or any other sequence whose elements have << defined for them.
#include <cstddef>
#include <fstream>
#include <iostream>
// Beware, brain-compiled code ahead!
template<class InpIt>
void save_seq(const std::ostream& os, InpIt begin, InpIt end)
{
if(begin != end)
os << *begin++;
while(begin != end)
os << ' ' << *begin++;
}
template<class OutpIt>
bool load_seq(const std::istream& is, OutpIt begin, std::size_t n)
{
for( std::size_t i=0; is && i<n; ++i)
is >> *begin++
return is.good() || is.eof();
}
template<class OutpIt>
bool load_seq(const std::istream& is, OutpIt begin)
{
while(is.good())
is >> *begin++
return is.eof();
}
template<class T, std::size_t N>
void save_array(const std::ostream& os, const T (&data)[N])
{
save_seq(os, data, data+N);
}
template<class T, std::size_t N>
bool load_array(const std::istream& is, T (&data)[N])
{
return load_seq(is, data, N);
}
int main()
{
const std::size_t size = 5;
int numbers[size];
numbers[0] = 10;
numbers[1] = 11;
numbers[2] = 12;
numbers[3] = 13;
numbers[4] = 14;
{
std::oftsream ofs("array.txt");
if(!ofs.good())
return 1;
save_array(ofs, numbers);
}
{
std::iftsream ifs("array.txt");
if(!ifs.good())
return 2;
int test[size];
load_array(ifs, test);
for (std::size_t idx = 0; idx < size; ++idx)
std::cout << "test[" << idx << "]: " << test[idx] << std::endl;
}
std::vector<int> numbers2;
numbers2.push_back(20);
numbers2.push_back(21);
numbers2.push_back(22);
numbers2.push_back(23);
{
std::oftsream ofs("array.txt");
if(!ofs.good())
return 1;
save_Seq(ofs, numbers2.begin(), numbers2.end());
}
{
std::iftsream ifs("array.txt");
if(!ifs.good())
return 2;
std::vector<int> test;
load_seq(ifs, std::back_inserter(test));
for (std::size_t idx = 0; idx < numbers2.size(); ++idx)
std::cout << "test[" << idx << "]: " << test[idx] << std::endl;
}
return 0;
}
You could use a std::fstream or boost::serialization. Your question is a bit vague, so I'm not entirely sure what it is you want, need?
If the array contains flat data (i.e., data that does not include pointers to other data), you can write an entire array in a single write to a file.
Without any idea what your data looks like, it would not be possible to say much more about it.
After some tinkering I came up with this:
#include <cstddef>
#include <fstream>
#include <iostream>
template<class T>
void SaveArray(const std::string & file, T * data, std::size_t length)
{
std::ofstream out(file.c_str());
for (std::size_t idx = 0; idx < length; ++idx)
{
if (idx != 0)
{
out << " ";
}
out << *data++;
}
}
template<class T>
std::size_t LoadArray(const std::string & file, T * data, std::size_t length)
{
std::ifstream in(file.c_str());
std::size_t count = 0;
while (count++ < length && in >> *data++);
return count - 1; // return number of items
}
int main()
{
int numbers[5];
numbers[0] = 10;
numbers[1] = 11;
numbers[2] = 12;
numbers[3] = 13;
numbers[4] = 14;
SaveArray("array.txt", &numbers[0], 5);
int test[5];
LoadArray("array.txt", &test[0], 5);
for (std::size_t idx = 0; idx < 5; ++idx)
{
std::cout << "test[" << idx << "]: " << test[idx] << std::endl;
}
return 0;
}
Suggestions for improvement are welcome.
Related
In the following code I want to print the whole array instead of printing each single value. Can we do that? I'm familiar with python where we append values but I can't find that in C++. So how to get desired result. What if I have to print it using printf?
#include <iostream>
using namespace std;
int *name(){
static int n[5];
for(int i=0;i<5;i++){
n[i]=i;}
return n;}
int main(){
int *p;
p=name();
for(int i=0;i<5;i++){
cout<<*(p+i)<<endl;}
return 0;
desired output should be -
[0,1,2,3,4]
#include <iostream>
using namespace std;
int *name() {
static int n[5];
for (int i = 0; i < 5; ++i)
n[i] = i;
return n;
}
int main()
{
int *p;
p = name();
cout << '[';
for (int i = 0; i < 5; ++i)
cout << *(p+i) << (i != 4 ? ',' : ']');
}
Or, if you want to encapsulate the array-printing into a general function:
#include <iostream>
#include <string>
using namespace std;
std::string PrintArray(int* array, size_t size) {
std::string answer = "[";
for (size_t i = 0; i < size; ++i) {
answer += std::to_string(array[i]);
answer += (i < size-1) ? "," : "]";
}
return answer;
}
int* name() {
static int n[5];
for (int i = 0; i < 5; i++) {
n[i] = i;
}
return n;
}
int main() {
int* p;
p = name();
cout << PrintArray(p, 5) << endl;
return 0;
}
I usually end up overloading operator<< for the desired type, but your mileage may vary.
#include <iostream>
#include <vector>
template <class InputIt>
std::ostream& print_impl(std::ostream& os, InputIt first, InputIt last)
{
os << '[';
if (first != last)
{
os << *first;
while ( ++first != last )
os << ", " << *first;
}
return os << ']';
}
template <class T>
std::ostream& operator<<(std::ostream& os, std::vector<T> const& v)
{
return print_impl(os, v.cbegin(), v.cend());
}
template <class T, size_t N>
std::ostream& operator<<(std::ostream& os, T const (&a)[N])
{
return print_impl(os, std::cbegin(a), std::cend(a));
}
int main()
{
std::vector<int> a{1, 2, 3, 4, 5};
std::cout << a << '\n';
float b[] = {3.14f, -0.1f, 2.0f};
std::cout << b << '\n';
}
I have implemented a heap data structure, and use it to sort. My understanding is that it is O(nlogn) complexity. However, when compared to bubble sort, it is order of magnitude slower -- and yeah, I tried running it for larger arrays. I checked some answers at SO (in particular this and this), but still lost. Could anyone point out what am I doing wrong here, please?
The results are:
HEAP SORT: 12415690ns
QUICK SORT: 71ns
BUBBLE SORT: 541659ns
Here are the codes:
main.cpp:
#include <chrono>
#include <iostream>
#include <stdexcept>
#include <vector>
// #include "heap.cpp"
// #include "pqueue.cpp"
#include "sort.cpp"
using namespace std;
using namespace std::chrono;
template <class T>
void printVector (vector<T> A) {
for (std::vector<int>::iterator it = A.begin(); it != A.end(); ++it) {
std::cout << *it << ' ';
}
cout << endl;
}
template <class T>
vector<T> constructVector(int A[], std::size_t len, std::size_t num) {
vector<T> res (A, A+len);
for (std::size_t idx = 0; idx < num-1; ++idx) {
res.push_back(A[idx%len]);
}
return res;
}
int main() {
high_resolution_clock::time_point t1;
high_resolution_clock::time_point t2;
int a[] = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7};
std::size_t len = sizeof(a) / sizeof(int);
vector<int> HEAP = constructVector<int>(a, len, 32000); // (a, a + sizeof(a) / sizeof(int));
vector<int> QUICK = constructVector<int>(a, len, 32000); // (a, a + sizeof(a) / sizeof(int));
vector<int> BUBBLE = constructVector<int>(a, len, 32000);
// cout << "Original Array: "; printVector(HEAP);
cout << "HEAP SORT: ";
t1 = high_resolution_clock::now();
heapsort(HEAP);
t2 = high_resolution_clock::now();
cout << duration_cast<nanoseconds>( t2 - t1 ).count() << "ns\n";
// cout << "New Array: "; printVector(HEAP);
// cout << "Original Array: "; printVector(QUICK);
cout << "QUICK SORT: ";
t1 = high_resolution_clock::now();
quicksort(QUICK, 0, QUICK.size());
t2 = high_resolution_clock::now();
cout << duration_cast<nanoseconds>( t2 - t1 ).count() << "ns\n";
// cout << "New Array: "; printVector(HEAP);
// cout << "Original Array: "; printVector(QUICK);
cout << "BUBBLE SORT: ";
t1 = high_resolution_clock::now();
bublesort(BUBBLE);
t2 = high_resolution_clock::now();
cout << duration_cast<nanoseconds>( t2 - t1 ).count() << "ns\n";
// cout << "New Array: "; printVector(HEAP);
}
sort.cpp:
#ifndef __SORT_CPP_INCLUDED_
#define __SORT_CPP_INCLUDED_
#include <vector>
#include "heap.cpp"
template <class T>
void heapsort(std::vector<T> &A, bool increasing = true) {
Heap<T> H(A, increasing);
H.sort();
A = H.get();
}
template <class T>
std::size_t partition(std::vector<T> &A, std::size_t p, std::size_t r) {
T x = A[r-1];
std::size_t i = p - 1;
for (std::size_t j = p; j < r; ++j) {
if (A[j] <= x) {
++i;
A[i] ^= A[j];
A[j] ^= A[i];
A[i] ^= A[j];
}
}
A[i+1] ^= A[r-1];
A[r-1] ^= A[i+1];
A[i+1] ^= A[r-1];
return i + 1;
}
template <class T>
void quicksort(std::vector<T> &A, std::size_t p, std::size_t r) {
if (p-1 < r) {
std::size_t q = partition(A, p, r);
quicksort(A, p, q);
quicksort(A, q+1, r);
}
}
template <class T>
void bublesort(std::vector<T> &A) {
bool swapped = false;
do {
swapped = false;
for (std::size_t idx = 1; idx < A.size(); ++idx) {
if (A[idx-1] > A[idx]) {
// swap them
A[idx] = A[idx-1];
A[idx-1] = A[idx];
A[idx] = A[idx-1];
swapped = true;
}
}
} while (swapped);
}
#endif
heap.cpp:
#ifndef __HEAP_CPP_INCLUDED__
#define __HEAP_CPP_INCLUDED__
#include <vector>
template <class T>
class Heap {
public:
Heap(bool maxHeap = true) : heap_size(0), max_heap(maxHeap) {}
Heap(const std::vector<T> &a, bool maxHeap = true) : A(a), max_heap(maxHeap) {
if (maxHeap) this->build_max_heap(); else this->build_min_heap(); }
~Heap() {}
protected:
std::vector<T> A;
std::size_t heap_size;
bool max_heap;
public:
std::size_t parent(std::size_t idx) { return (idx - 1) >> 1; }
std::size_t left(std::size_t idx) { return (idx << 1) + 1; }
std::size_t right (std::size_t idx) { return (idx + 1) << 1; }
public:
std::vector<T> get() { return A; }
std::size_t size() { return heap_size; }
void sort();
void build_max_heap();
void build_min_heap();
void max_heapify(std::size_t idx);
void min_heapify(std::size_t idx);
};
template <class T>
void Heap<T>::sort() {
if (this->heap_size <= 0) return; // Already sorted or empty
if (this->heap_size != this->A.size()){ // Not sorted and not heapified
max_heap ? build_max_heap() : build_min_heap();
}
for (std::size_t idx = this->A.size()-1; idx > 0; --idx) {
A[0] ^= A[idx];
A[idx] ^= A[0];
A[0] ^= A[idx];
--this->heap_size;
max_heap ? max_heapify(0) : min_heapify(0);
}
}
template<class T>
void Heap<T>::build_max_heap() {
this->heap_size = this->A.size();
for (std::size_t idx = (this->A.size() - 1) >> 1; idx > 0; --idx)
this->max_heapify(idx);
this->max_heapify(0);
}
template<class T>
void Heap<T>::build_min_heap() {
this->heap_size = this->A.size();
for (std::size_t idx = (this->A.size()-1) >> 1; idx > 0; --idx)
this->min_heapify(idx);
this->min_heapify(0);
}
template <class T>
void Heap<T>::max_heapify(std::size_t idx) {
std::size_t l = this->left(idx);
std::size_t r = this->right(idx);
std::size_t largest;
if (l < this->heap_size && A[l] > A[idx]) largest = l;
else largest = idx;
if (r < this->heap_size && A[r] > A[largest]) largest = r;
if (largest != idx) {
this->A[idx] ^= this->A[largest];
this->A[largest] ^= this->A[idx];
this->A[idx] ^= this->A[largest];
this->max_heapify(largest);
}
}
template <class T>
void Heap<T>::min_heapify(std::size_t idx) {
std::size_t l = this->left(idx);
std::size_t r = this->right(idx);
std::size_t smallest;
// std::cout << "DEBUG: " << idx << std::endl;
if (l < this->heap_size && A[l] < A[idx]) smallest = l;
else smallest = idx;
if (r < this->heap_size && A[r] < A[smallest]) smallest = r;
if (smallest != idx) {
this->A[idx] ^= this->A[smallest];
this->A[smallest] ^= this->A[idx];
this->A[idx] ^= this->A[smallest];
this->min_heapify(smallest);
}
}
#endif
Your bubble sort doesn't swap, but only copy. This would make it somewhat faster. Not sure this alone explains being so fast, though.
Your Heap<T> makes a copy of the array. This can explain the slowness. I guess you forgot a &.
You should have noticed 71ns to sort 32k array is not real. Your quicksort never sorts anything. You can use std::sort for a reliable quicksort.
Too much is known at compile time and the numbers are far from random. Switch to arrays with random numbers in this kind of testing.
Here is some random sort program I wrote in C++. It works pretty fine for 10 elements or so. But for 15 elements it works so slow I can't even wait enough to get the result. Is there some way to optimize random sort algorithm?
Here's my code:
// randomsort.h
#ifndef RANDOMSORT_H
#define RANDOMSORT_H
#include <stdlib.h>
#include <time.h>
class RandomSort
{
private:
template <class T>
static bool isOrdered(T*, int);
public:
template <class T>
static int sort(T*, int);
};
template <class T>
bool RandomSort::isOrdered(T* arr, int size)
{
for(int i = 1; i < size; i++)
{
if(arr[i-1] > arr[i])
{
return false;
}
}
return true;
}
template <class T>
int RandomSort::sort(T* arr, int size)
{
int stepAmount = 0;
srand(time(NULL));
while(!isOrdered(arr, size))
{
int i = rand() % size;
int j = rand() % size;
std::swap(arr[i], arr[j]);
stepAmount++;
}
return stepAmount;
}
#endif // RANDOMSORT_H
And main.cpp file
// main.cpp
#include <iostream>
#include "randomsort.h"
int main()
{
int size;
std::cout << "Enter amount of elements to sort: ";
std::cin >> size;
std::cout << std::endl;
int arr[size];
for(int i = 0; i < size; i++)
{
arr[i] = (rand() % (size * 10));
}
std::cout << "Input array: " << std::endl;
for(int i = 0; i < size; i++)
std::cout << arr[i] << ' ';
std::cout << std::endl << std::endl;
int stepAmount = RandomSort::sort(arr, size);
std::cout << "Output array: " << std::endl;
for(int i = 0; i < size; i++)
std::cout << arr[i] << ' ';
std::cout << std::endl << std::endl;
std::cout << "Number of steps: " << stepAmount;
return 0;
}
Any suggestions?
Your code is completely random. So it can swap when it should not. An easy fix would be to swap only if you need it.
int i = rand() % size;
int j = rand() % size;
// to know which should be first
if (i > j)
std::swap(i, j);
if (arr[i] > arr[j])
std::swap(arr[i], arr[j]);
Your array probably will not be sorted immediately, so you could also test if it is sorted only every five steps (for example) instead of every step.
But i think the most important is, you should not expect good performances from such an algorithm.
This question already has answers here:
Library function for Permutation and Combination in C++
(5 answers)
Closed 8 years ago.
I am looking for the implementation of Permutation, Combination and PowerSet using C+++
Using STL:
Permutation:
using std::next_permutation
template <typename T>
void Permutation(std::vector<T> v)
{
std::sort(v.begin(), v.end());
do {
std::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, " "));
std::cout << std::endl;
} while (std::next_permutation(v.begin(), v.end()));
}
Combination:
template <typename T>
void Combination(const std::vector<T>& v, std::size_t count)
{
assert(count <= v.size());
std::vector<bool> bitset(v.size() - count, 0);
bitset.resize(v.size(), 1);
do {
for (std::size_t i = 0; i != v.size(); ++i) {
if (bitset[i]) {
std::cout << v[i] << " ";
}
}
std::cout << std::endl;
} while (std::next_permutation(bitset.begin(), bitset.end()));
}
PowerSet:
Note that if the size if less than the number of bit of your integer, you may you that integer instead of vector<bool>. If the size is known at compile time, prefer std::bitset<N> over std::vector<bool>
bool increase(std::vector<bool>& bs)
{
for (std::size_t i = 0; i != bs.size(); ++i) {
bs[i] = !bs[i];
if (bs[i] == true) {
return true;
}
}
return false; // overflow
}
template <typename T>
void PowerSet(const std::vector<T>& v)
{
std::vector<bool> bitset(v.size());
do {
for (std::size_t i = 0; i != v.size(); ++i) {
if (bitset[i]) {
std::cout << v[i] << " ";
}
}
std::cout << std::endl;
} while (increase(bitset));
}
Live example
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
void
Permutation(vector < char >set, vector < char >path, vector < bool > visited)
{
if (set.size() == path.size()) {
copy(path.begin(), path.end(), ostream_iterator < char >(cout, " "));
cout << endl;
return;
}
for (size_t i = 0; i < set.size(); ++i) {
if (!visited[i]) {
visited[i] = true;
path.push_back(set[i]);
Permutation(set, path, visited);
visited[i] = false;
path.pop_back();
}
}
}
void
Combination(vector < char >set, vector < char >path, size_t start, size_t maxlen)
{
if (maxlen == path.size()) {
copy(path.begin(), path.end(), ostream_iterator < char >(cout, " "));
cout << endl;
return;
}
for (size_t i = start; i < set.size(); ++i) {
path.push_back(set[i]);
Combination(set, path, ++start, maxlen);
path.pop_back();
}
}
void
PowerSet(vector < char >set)
{
for (int i = 0; i <= set.size(); ++i) {
vector < char >path;
Combination(set, path, 0, i);
}
}
int
main()
{
vector < char >vc {
'A', 'B', 'C', 'D'
};
vector < char >path;
vector < bool > visited(4, false);
cout << "\n------PERMUTATION----------\n";
Permutation(vc, path, visited);
cout << "\n------COMBINATION----------\n";
Combination(vc, path, 0, 3);
cout << "\n------POWERSET-------------\n";
PowerSet(vc);
return 0;
}
This might seem like a stupid question but it's really bugging me.
Basically, I have a 1D array that I need to convert into a 2D array. Basically, the size of the array is: 62017 now from this, I need to get the Rows and Cols of this. But, do this dynamically, so for example, it would take the number of say: 43101 and then establish the rows+cols and then re-size the vector accordingly.
I hope I've explained enough and hope someone can help, thanks :)
And here it is old school array style (since I can't access a C++0x compiler at the moment)
#include <iostream>
//#include <array>
#include <inttypes.h>
#include <math.h>
void calc_new_sizes(const size_t old_size, size_t& new_size1, size_t& new_size2)
{
new_size1 = 1;
new_size2 = 1;
size_t stop_at = (size_t)sqrt(old_size) + 1;
for (size_t i = 1; i<stop_at; i++)
{
if ( old_size % i == 0 )
{
new_size1 = i;
new_size2 = old_size / i;
}
}
}
template <class T>
T** twoDimensionify(T* p_old_array, const size_t old_size)
{
size_t new_size1=1, new_size2=1, old_i=0;
calc_new_sizes(old_size, new_size1, new_size2);
T** returnValue = new T*[new_size1];
for (size_t i=0; i<new_size1; i++)
{
returnValue[i] = new T[new_size2];
for (size_t j=0; j<new_size2; j++)
{
returnValue[i][j] = p_old_array[old_i];
old_i++;
}
}
return returnValue;
}
int main()
{
size_t old_size=20, new_size1=0, new_size2=0;
calc_new_sizes(old_size, new_size1, new_size2);
std::cout << "From " << old_size << " to " << new_size1 << "x" << new_size2 << std::endl;
int old_array[20] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
int **new_array = twoDimensionify<int>(old_array, 20);
for (size_t i=0; i<new_size1; i++)
{
for (size_t j=0; j<new_size2; j++)
{
std::cout << "new array[" << i << "," << j << "] = " << new_array[i][j] << std::endl;
}
}
// Clean up my memory. This is C++ afterall.
for (size_t i=0; i<new_size1; i++)
{
delete [](new_array[i]);
}
delete []new_array;
return 0;
}
I believe you're looking for something like this. Though with some challenges accessing a proper compiler I can't verify...
#include <iostream>
#include <array>
#include <inttypes.h>
#include <math.h>
void calc_new_sizes(const size_t old_size, size_t& new_size1, size_t& new_size2)
{
new_size1 = 1;
new_size2 = 1;
size_t stop_at = (size_t)sqrt(old_size) + 1;
for (size_t i = 1; i<stop_at; i++)
{
if ( old_size % i == 0 )
{
new_size1 = i;
new_size2 = old_size / i;
}
}
}
template <class T, size_t new_size_1, size_t new_size_2, size_t old_size>
std::array<new_size_1, std::array<T, new_size_2>> twoDimensionify(std::array<T, old_size> p_input)
{
std::array<new_size_1, std::array<T, new_size_2>> returnValue;
int old_i = 0;
for (int i=0; i<new_size_1; i++)
{
for (int j=0; j<new_size_2; j++)
{
returnValue[i][j] = p_input[old_i];
old_i++;
}
}
return returnValue;
}
int main()
{
size_t old_size=20, new_size1=0, new_size2=0;
calc_new_sizes(old_size, new_size1, new_size2);
std::cout << "From " << old_size << " to " << new_size1 << "x" << new_size2 << std::endl;
return 0;
}