Choose at runtime array or vector in C++ - c++

I have a problem described as below ::
class datad {
private:
int *a;
int _size;
vector<int> v;
public:
datad(int arr[], int size) {
_size = size;
for (int i = 0; i < size; i++)
a[i] = arr[i];
}
datad(vector<int> ve)
{
v = ve;
_size = ve.size();
}
void printdata()
{
// print data which the object has been initialized with
// if object is initialized with vector then print vector
// array print array
}
};
int main()
{
// print only vector data
int a[] = { 9,4,8,3,1,6,5 };
datad d(v1);
d.printdata();
// print only array data
datad d1(a, 7);
d1.printdata();
}
I need to find the way the object is initialized and then based on the same should be able to printdata accrodingly.
Can someone help me understand if it is possible at all?

Add a bool usesVector to your class and set it to true or false in each constructor as appropriate. Then, in printdata, simply check the value of the boolean.
Or you can set size to -1 in the vector case (as it's otherwise unused) and just check for that.
By the way, your array implementation is broken, because you never allocate any memory for it. You'd be much better off using only the vector version. You can still initialise that vector from array data if you wish.

You can set a flag in respective constructor and check that flag during the printing method.
I hope this is for learning purposes, otherwise as noted you maybe better of using just the vector version. When using dynamic memory management in class you need to be aware of things like rule of three and I guess there is also rule of five.

Related

How to get the length of a class array in C++, without hardcoding?

I have a class array in C++. I have defined it as such:
Student* pliststudent = new Student[2]{ 3,5 };
I know that the class array decays into a pointer when we assign it to Student* pliststudent.
It becomes difficult to extract the length of the class array, without hardcoding it.
I implemented the code, such that it is not hardcoded (using friend function). But I think, a better solution must exist.
Following is my full code:
class Student
{
private:
int marks;
int counter=0;
public:
Student(int marks) : marks(marks)
{}
int FinalMarks()
{
marks *=2;
return marks;
}
friend int Length(Student ob1, Student ob2);
};
int Length(Student ob1, Student ob2)
{
int i = ++ob1.counter + ++ob2.counter;
return i;
}
int main()
{
Student* pliststudent = new Student[2]{ 3,5 };
//int length = sizeof(*pliststudent) / sizeof(pliststudent[0]); //This does not work.
int length = Length(pliststudent[0], pliststudent[1]);
for (int i = 0; i < length; i++)
{
cout << (pliststudent+i)->FinalMarks() << endl;
}
return 0;
}
Is there a better solution to this? I don't want to hardcode the size of the Student class array.
You cannot get the length of an array from a pointer to the array's first element. That information is lost when the array decays to a pointer.
You need to keep the length information somewhere, either yourself:
int length = 2;
Student* pliststudent = new Student[length]{ 3,5 };
or by using a container that tracks the length for you:
std::vector<Student> students{3, 5};
// students.size() returns the number of Student objects in the vector
Live Demo
If you must use a dynamic array, there is no way to automatically determine the size of the array. That information is "lost" when storing the array in a pointer as you have done.
There are several ways to work around this, the preferred one is the one already suggested in the comments, use std::vector<Student> instead. Vector is the standard library's dynamic array and is almost always what you want. And since it takes care of the memory management automatically, it is much simpler to work with.
However, if you must use dynamic arrays, (because this is a school assignment and you are not allowed to use std::vector), then you should store the size of the array in a separate variable, and pass that together with whatever you need to do.
const int nStudents = 2;
Student* pliststudents = new Student[nStudents]{3, 5};

How to have a 2-dimensional array as a private variable in a class, and then set it in the constructor

In my platformer game which I'm writing in Visual C++, each level will initially be stored as a 2-dimensional array of ints. I decided it would make more sense to store this array in a class, so I created a class called Level. It looks like this:
class Level {
private:
int map[20][30];
public:
Level(int a[20][30]) {
map = a;
}
int getcell(int row, int column) {
return map[row][column];
}
};
As far as I can see - from looking up tutorials on class constructors, and passing 2-dimensional arrays as parameters, this should work, so I really don't understand why it doesn't.
On the line where I do map = a, I get an error: Error: expression must be a modifiable lvalue. I've looked this error up on stackoverflow, but I can't find any answers which relate to my problem.
So, how can I fix this error?
This doesn't really have anything to do with a constructor. You cannot assign arrays in C++. Whether in the constructor, or anywhere else.
There are two ways to work around it. The first way is the brute force way. Instead of
map = a;
write a loop to copy the contents of the array from the constructor's parameter into the class member array.
The second way is to stuff the array into an intermediate class:
class Level {
public:
struct level_map {
int map[20][30];
};
private:
level_map map;
public:
Level(const level_map &initial_map) : map(initial_map)
{
}
int getcell(int row, int column) {
return level_map.map[row][column];
}
};
This may or may not be practical, and introduces a little bit more complexity.
But the real answer here is to use std::vectors instead of plain arrays, which will solve all of these problems.
Others have already mentioned the real reason: you cannot assign an array to another using = operator. My two cents about your class:
map is not a good name, it may get conflict with std::map if using namespace std; or using std::map was specified somewhere.
The constant array sizes make this class non-reusable. Class should be flexible to allow any N*M sized 2D array. For this, better to use vector<vector<int>>.
getcell should be a const method, and it should do error checking with row and column numbers passed.
If you want this class to have static-sized array sizes and compile time, you may use class templates with row and column sizes as non type template arguments.
template<size_t row, size_t column>
class Level
{
int _map[row][column];
public:
Level(int src[row][column])
{
memcpy(_map, src, sizeof(_map)); // why not simply 'memcpy' ?
}
};
int main()
{
int source[10][2] = { {1, 2}, {3,4} };
Level<10, 2> ten_by_2(source);
}
Here the map is a constant value, which could not been assigned as an lvalue. This could be fixed by iterating the element of the array, and assign a[i][j] to map[i][j].
class Level {
private:
int map[20][30];
public:
Level(int a[20][30]) {
for(int i = 0; i < 20; ++i)
for(int j = 0; j < 30; ++j)
map[i][j] = a[i][j];
}
int getcell(int row, int column) {
return map[row][column];
}
};

Pass by value, no deep copy on assignment with std::vector?

Understanding std::set.insert & std::vector behavior.
Please consider the following scenario:
A.h
class A {
uint id;
vector<double> values;
operator<(const A& argA) const;
}
A.cpp
A::A(uint argId, vector<double> argValues) {
this->id = argId;
this->values = argValues;
}
A::operator<(const A& argA) const {
// it's guaranteed that there's always at least one element in the vector
return this->values[0] < argA.values[0];
}
B.cpp
std::set<A> mySet;
for (uint i = 0; i < (uint) 10; i++)
{
vector<double> tempVector(3);
for (uint j = 0; j < (uint) 3; j++) {
tempVector[j] = j;
}
myset.insert(A(i + 1, tempVector));
}
In my understanding, tempElement owns a deep copied vector (values), because the vector was passed by value in its constructor and assigned. Therefore looping over i shouldn't break the added elements to my set. BUT inserting *tempElement breaks - SIGSEV. In my logic this should work... Every help appreciated!
EDIT: the code crashes during the insertion process (second element); set invokes the LT-operator, tries to access the vector of the passed argument - but cannot. Before the creation of A where I pass the id and the vector I check if the passed vector contains the right elements.
For a small vector it shouldn't matter, but if you have a large array and it will be expensive to keep copying it, yourA should contain some kind of pointer that shallow-copies. There are several options:
boost::shared_array<double>
boost::shared_ptr<vector<double> >
boost::shared_ptr<double> but with array deleter passed in on construction.
Make A non-copyable and have a set of (shared) pointers to A with some comparison functor that compares what is in the pointers rather than the pointers themselves.
Note that with either shared_array or shared_ptr you won't be able to extract the size (number of elements) so you would have to store that separately.
I don't think the problem is in this code. However I notice you have a vector tempVector but you assing the values to tempComponents instead. I can't see tempComponents declaration but my guess is it is of different size.
Working code with numerous changes - but I don't see the problem that you were describing.
#include <set>
#include <vector>
using namespace std;
typedef unsigned int uint;
class A {
public:
A(uint argId, vector<double> argValues)
{
this->id = argId;
this->values = argValues;
}
bool operator < ( A const& a ) const
{
return a.id < id;
}
uint id;
vector<double> values;
};
int _tmain(int argc, _TCHAR* argv[])
{
std::set<A> mySet;
for (uint i = 0; i < (uint) 10; i++)
{
vector<double> tempVector(3);
for (uint j = 0; j < (uint) 3; j++) {
tempVector[j] = j;
}
std::unique_ptr<A> tempElement(new A(i + 1, tempVector));
mySet.insert(*tempElement);
}
return 0;
}
No, there's no reason for inserting into myset here to cause a crash. The problem must lie elsewhere. Perhaps in A's copy ctor if you're not using the default one.
However your code is leaking memory. When you insert into the set *tempElement is copied into the set, and then the original that you allocated with new is no longer used but is never deleted. Instead you could just do A tempElement(i+1,tempVector); so that after the object is copied into the set it gets properly destroyed. Or perhaps better in this case you could just construct it as a temporary passed directly to insert: myset.insert(A(i+1,tempVector)) in which case the object will be moved instead of copied, reducing the overhead. Or you could just construct the object in place to avoid even moving: myset.emplace(i+1,tempVector);
Also I'm assuming that by tempComponents[j] = j; you meant tempVector[j] = j. You could replace that loop with std::iota(begin(tempVector),end(tempVector),0). edit: or you could use the new initializer syntax Furthermore, since the vector is the same everytime you could use just one outside the loop:
vector<double> tempVector(3) = {0.0,1.0,2.0}
std::set<A> mySet;
for (uint i = 0; i < (uint) 10; i++)
{
myset.emplace(i+1,tempVector);
}
C++03 compilers won't support emplace or the new initializer syntax, and iota would be a compiler extension for them (it's from the original SGI STL, so some may have it). For those you would still use insert and use a for loop to initialize tempVector or use an array:
double tempVector_init[] = {0.0,1.0,2.0};
vector<double> tempVector(tempVector_init,tempVector_init+3);
std::set<A> mySet;
for (uint i = 0; i < (uint) 10; i++)
{
myset.insert(A(i+1,tempVector));
}

C++: joining array together - is it possible with pointers WITHOUT copying?

as in the title is it possible to join a number of arrays together without copying and only using pointers? I'm spending a significant amount of computation time copying smaller arrays into larger ones.
note I can't used vectors since umfpack (some matrix solving library) does not allow me to or i don't know how.
As an example:
int n = 5;
// dynamically allocate array with use of pointer
int *a = new int[n];
// define array pointed by *a as [1 2 3 4 5]
for(int i=0;i<n;i++) {
a[i]=i+1;
}
// pointer to array of pointers ??? --> this does not work
int *large_a = new int[4];
for(int i=0;i<4;i++) {
large_a[i] = a;
}
Note: There is already a simple solution I know and that is just to iteratively copy them to a new large array, but would be nice to know if there is no need to copy repeated blocks that are stored throughout the duration of the program. I'm in a learning curve atm.
thanks for reading everyone
as in the title is it possible to join a number of arrays together without copying and only using pointers?
In short, no.
A pointer is simply an address into memory - like a street address. You can't move two houses next to each other, just by copying their addresses around. Nor can you move two houses together by changing their addresses. Changing the address doesn't move the house, it points to a new house.
note I can't used vectors since umfpack (some matrix solving library) does not allow me to or i don't know how.
In most cases, you can pass the address of the first element of a std::vector when an array is expected.
std::vector a = {0, 1, 2}; // C++0x initialization
void c_fn_call(int*);
c_fn_call(&a[0]);
This works because vector guarantees that the storage for its contents is always contiguous.
However, when you insert or erase an element from a vector, it invalidates pointers and iterators that came from it. Any pointers you might have gotten from taking an element's address no longer point to the vector, if the storage that it has allocated must change size.
No. The memory of two arrays are not necessarily contiguous so there is no way to join them without copying. And array elements must be in contiguous memory...or pointer access would not be possible.
I'd probably use memcpy/memmove, which is still going to be copying the memory around, but at least it's been optimized and tested by your compiler vendor.
Of course, the "real" C++ way of doing it would be to use standard containers and iterators. If you've got memory scattered all over the place like this, it sounds like a better idea to me to use a linked list, unless you are going to do a lot of random access operations.
Also, keep in mind that if you use pointers and dynamically allocated arrays instead of standard containers, it's a lot easier to cause memory leaks and other problems. I know sometimes you don't have a choice, but just saying.
If you want to join arrays without copying the elements and at the same time you want to access the elements using subscript operator i.e [], then that isn't possible without writing a class which encapsulates all such functionalities.
I wrote the following class with minimal consideration, but it demonstrates the basic idea, which you can further edit if you want it to have functionalities which it's not currently having. There should be few error also, which I didn't write, just to make it look shorter, but I believe you will understand the code, and handle error cases accordingly.
template<typename T>
class joinable_array
{
std::vector<T*> m_data;
std::vector<size_t> m_size;
size_t m_allsize;
public:
joinable_array() : m_allsize() { }
joinable_array(T *a, size_t len) : m_allsize() { join(a,len);}
void join(T *a, size_t len)
{
m_data.push_back(a);
m_size.push_back(len);
m_allsize += len;
}
T & operator[](size_t i)
{
index ix = get_index(i);
return m_data[ix.v][ix.i];
}
const T & operator[](size_t i) const
{
index ix = get_index(i);
return m_data[ix.v][ix.i];
}
size_t size() const { return m_allsize; }
private:
struct index
{
size_t v;
size_t i;
};
index get_index(size_t i) const
{
index ix = { 0, i};
for(auto it = m_size.begin(); it != m_size.end(); it++)
{
if ( ix.i >= *it ) { ix.i -= *it; ix.v++; }
else break;
}
return ix;
}
};
And here is one test code:
#define alen(a) sizeof(a)/sizeof(*a)
int main() {
int a[] = {1,2,3,4,5,6};
int b[] = {11,12,13,14,15,16,17,18};
joinable_array<int> arr(a,alen(a));
arr.join(b, alen(b));
arr.join(a, alen(a)); //join it again!
for(size_t i = 0 ; i < arr.size() ; i++ )
std::cout << arr[i] << " ";
}
Output:
1 2 3 4 5 6 11 12 13 14 15 16 17 18 1 2 3 4 5 6
Online demo : http://ideone.com/VRSJI
Here's how to do it properly:
template<class T, class K1, class K2>
class JoinArray {
JoinArray(K1 &k1, K2 &k2) : k1(k1), k2(k2) { }
T operator[](int i) const { int s = k1.size(); if (i < s) return k1.operator[](i); else return k2.operator[](i-s); }
int size() const { return k1.size() + k2.size(); }
private:
K1 &k1;
K2 &k2;
};
template<class T, class K1, class K2>
JoinArray<T,K1,K2> join(K1 &k1, K2 &k2) { return JoinArray<T,K1,K2>(k1,k2); }
template<class T>
class NativeArray
{
NativeArray(T *ptr, int size) : ptr(ptr), size(size) { }
T operator[](int i) const { return ptr[i]; }
int size() const { return size; }
private:
T *ptr;
int size;
};
int main() {
int array[2] = { 0,1 };
int array2[2] = { 2,3 };
NativeArray<int> na(array, 2);
NativeArray<int> na2(array2, 2);
auto joinarray = join(na,na2);
}
A variable that is a pointer to a pointer must be declared as such.
This is done by placing an additional asterik in front of its name.
Hence, int **large_a = new int*[4]; Your large_a goes and find a pointer, while you've defined it as a pointer to an int. It should be defined (declared) as a pointer to a pointer variable. Just as int **large_a; could be enough.

deleting an array that stores pointers to some objects

I am storing pointers to elements of a vec_A in an array A* a_ptrs[3] . Assume that vec_A will not be resized. So, a_ptrs[i] will point to the correct element.
My question is:
Suppose A* a_ptrs[3] is declared in a class B. Since it is not created using 'new' I am guessing I don't need to delete it in the destructor. Am I right??
class A {
public:
int getNumber();
A(int val);
~A(){};
private:
int num;
};
A::A(int val){
num = val;
};
int A::getNumber(){
return num;
};
int main(){
int i =0;
int num;
std::vector<A> vec_A;
for ( i = 0; i < 10; i++){
vec_A.push_back(A(i));
}
A* a_ptrs[3];
a_ptrs[0] = &vec_A[0];
a_ptrs[1] = &vec_A[3];
a_ptrs[2] = &vec_A[5];
for (i = 0; i<3; i++){
std::cout<<"\n: a_ptrs[i].getNumber() = "<<a_ptrs[i]->getNumber();
}
std::cout << "\nPress RETURN to continue...";
std::cin.get();
return 0;
}
Yep, thats correct. You don't need to use delete. The only issue is if the vector is resized e.g. by calling push_back etc - but you called that out in your post.
Yes, delete is used only for variables allocated with new.
Correct, since there is no dynamic memory allocation in the program.
My Suggestion is to use vector.reserve() function to reserve the size of the vector which will improve program performance.
Basically when you add an element to CArray(MFC) or std::vector it reallocates necessary memory and copies the elements so it will lead to memory fragmentation and will degrade program speed.