Adding element to Array of Objects in C++ - c++

How do I add an element to the end of an array dynamically in C++?
I'm accustomed to using vectors to dynamically add an element. However, vectors does not seem to want to handle an array of objects.
So, my main goal is having an array of objects and then being able to add an element to the end of the array to take another object.
EDIT**
Sorry, its the pushback() that causes me the problems.
class classex
{
private:
int i;
public:
classex() { }
void exmethod()
{
cin >> i;
}
};
void main()
{
vector <classex> vectorarray;
cout << vectorarray.size();
cout << vectorarray.push_back();
}
Now I know push_back must have an argument, but What argument?

Now I know push_back must have an argument, but What argument?
The argument is the thing that you want to append to the vector. What could be simpler or more expected?
BTW, you really, really, really do not want exmethod as an actual method of classex in 99% of cases. That's not how classes work. Gathering the information to create an instance is not part of the class's job. The class just creates the instance from that information.

Arrays are fixed sized containers. So enlarging them is not possible. You work around this and copy one array in a bigger and gain space behind the old end, but that's it.
You can create a array larger than you currently need it and remember which elements are empty. Of course they are never empty (they at least contain 0's), but that's a different story.
Like arrays, there are many containers, some are able to grow, like the stl containers: lists, vectors, deques, sets and so on.
add a Constructor to set i (just to give your example a real world touch) to your example classex, like this:
class classex {
public:
classex(int& v) : i(v) {}
private:
int i;
};
An example for a growing container looks like this:
vector <classex> c; // c for container
// c is empty now. c.size() == 0
c.push_back(classex(1));
c.push_back(classex(2));
c.push_back(classex(3));
// c.size() == 3

EDIT: The question was how to add an element to an array dynamically allocated, but the OP actually mean std::vector. Below the separator is my original answer.
std::vector<int> v;
v.push_back( 5 ); // 5 is added to the back of v.
You could always use C's realloc and free. EDIT: (Assuming your objects are PODs.)
When compared to the requirement of manually allocating, copying, and reallocating using new and delete, it's a wonder Stroustrup didn't add a keyword like renew.

Related

C++ : Null pointers and structs

If I want to make a struct for representing an item to render on the screen, and contains:
A list of natives "renderables" (meshes, triangles, etc)
A pointer to others sub-structures
Does this:
struct RenderGroup {
std::vector<RenderGroup*> Children;
std::vector<Renderable> Renderables;
}
What is the best way to do this? And if I have no sub-structures, can I just put a null pointer in the vector ?
Edit : Bad tags
A std::vector is not fixed size, so if there are no elements in it, size will return 0 and empty will return true.
std::vector<int> v;
auto sz = v.size(); // sz has value 0
auto e = v.empty(); // true
v.push_back(42);
sz = v.size(); // 1
e = v.emi empty(); // false
You can also remove elements (the vector moves all the followingv elements down to fill the gap)
std::vector<int> v { 1, 2, 3 };
auto it = v.find(2);
if (it != v.end())
v.erase(it); // v now contains 1, 3
So, in theory, no you don't need to put nullptrs in your vector. This assumes you've designed your application in a way that doesn't require a certain population size in your vector; if so, vector might not be the right container. Consider std::array.
There is a problem with your renderables vector, though: the vector stores instances of Renderable. The elements I'm the vector are exactly the size of a Renderable. So if you try and store a derived class, e.g Mesh, you will lose the derived classes extra fields.
This is called slicing. https://en.m.wikipedia.org/wiki/Object_slicing
You'll need to store pointers or references to the instances of the objects. Which depends on lifetime and ownership of the instances.
If your vector owns the remediables, you could use std::vector<std::unique_pre<Remediable>>.
If you are being given the instances from your caller, perhaps std::shared_ptr.
There's no need to put arbitrary "nothing" records in a std::vector, this isn't like a variable-length C-style array where you must terminate with some kind of delimiter. Just leave it empty.
In general practice you want to avoid putting NULL into lists like this where something might iterate over the contents and re-reference that pointer, causing a crash.
If RenderGroup is a base class and your Children vector is storing arbitrary children classes of RenderGroup then this approach is "fine" (though smart pointers would probably be preferred).
However, as tadman mentioned, if you're using pointers to represent the absense of a RenderGroup, you'll probably want to instead just remove the items in the vector when they are no longer needed.
See if this structure can help:
struct RenderGroup {
std::map<Renderable, std::vector<RenderGroup*>> Renderables;
}
List of child (RenderGroup) objects are mapped to Renderable object.

Container with non linear contents

I apologize if this question has been asked/answered elsewhere, but I didn't find anything since I'm not entirely sure what/how to ask this...
What I'm attempting to do is set up some kind of container; list, array, vector, what-ever, that will allow me to place and remove objects from specific indices.
Something like this:
[empty][empty][object][empty][object][object][empty]
I'm going to be moving objects from a vector into a specific index of this container and from this container to another vector.
What would be the best way to represent this and what kind of container would be best suited? I was originally using a vector, but the built in functions didn't seem to give me enough control over where the object ended up. It was either the front or the back.
I need to figure out the best way to hold those "empty" indices and move objects in and out of each element freely.
A simple, definitely sub-optimal, but quite effective solution could be to use a vector in the following way:
#include <iostream>
#include <vector>
using namespace std;
struct Your_Object
{
Your_Object& operator=(const Your_Object& other)
{
// Write a proper assignment operator here if you want to assign or swap values
cout << "hello from assignment operator"<<endl;
return *this;
}
};
int main() {
Your_Object nullObj;
std::vector<Your_Object> vec;
vec.reserve(10); // Creates 10 empty objects calling default constructors. Notice that this will NOT affect the vector's size, for that use resize()
Your_Object space5, space3; // Two objects to put in space5 and space3
// Put objects in space 5 and 3
vec[5] = space5;
vec[3] = space3;
// Move object in space 5 to another place
vec[1] = vec[5];
return 0;
}
http://ideone.com/YDu6LC
If you manage to write a proper copy-assignment operator (perhaps with move semantics if you're using C++11) which does a deep-copy of your object and if of course this proves to be not too burdensome for you, the above might be a simple working system for what you need to do.
Just remember to resize (or reserve, for the difference take a look here: https://stackoverflow.com/a/7397862/1938163) the space you need in advance.
If I understand your question correctly you want to place data in your vector according to a certain pattern.
You can use a simple vector and implement functions yourself to place your data.
For example if you want to place data in every third place :
void placeItem(std::vector<int> my_vector, int element, unsigned int index){
my_vector[((index+1)*3)-1]=element;
}
int retreiveItem(std::vector<int> my_vector, unsigned int index){
return my_vector[((index+1)*3)-1];
}
Then you can use placeItem and retreiveItem with indexes starting at 0.
If you simply meant that you want to place your data in arbitrary locations then you can use the [] syntax directly.

How to store an unknown number of elements in an array C++

Sorry if the title is not clear but I ll explain now my problem. I am new in C++.
I have created a class in C++. Instances of that class are the input of the program and they have to be stored in an array to perform the calculations. The problem is that the number of instances of that class that have to be defined by the user is fixed for a single run but can vary from run to run. Here an example:
#include <<blablah>blahblah>
int main()
{
int number_of_instances = 3;
MyClass first_instance(one_parameter_1, another_parameter_1);
MyClass second_instance(one_parameter_2, another_parameter_2);
MyClass third_instance(one_parameter_3, another_parameter_3);
///////////////////
NOW I HAVE TO STORE ALL THREE IN AN ARRAY LIKE
MyClass array[number_of_instances] = {first_instance, second_instance, third_instance};
THE PROBLEM IS THAT I DO NOT KNOW BEFORE HAND HOW MANY OF THEM ARE THE USER IS GOING TO INPUT
///////////////////
performCalculations(array);
return 0;
}
Thanks a lot in advance.
The typical C++ solution is to use a vector.
vector<MyClass> v;
v.push_back(first_instance); //add an already instantiated object to the end of the vector
v.push_back(second_instance);
v.push_back(third_instance);
You won't have to worry about memory management and you are able to access the vector like you would a normal array:
v[0].classMember
You can also add items to the vector in a loop if needed like so:
for(int i = 0; i < 5; i++){
v.push_back( MyClass(i, param2) );
}
And all the objects will be destructed when the vector goes out of scope if you're storing the objects directly in the vector.
One of the downsides to storing the objects directly in the vector is passing the vector as a parameter to a function. This will be a slow operation since the vector (and all the objects it holds) will have to be copied.
If you know the number of instances before you read them all in then you can allocate an array on the heap using new[]. (Don't forget to delete[] them when you've finished.) Note that this requires that the object have a default constructor.
you should use std::vector in this case rather than a built-in array.
#include <vector>
...
std::vector<MyClass> v = {first_instance, second_instance, third_instance};
...
v.push_back(fourth_instance);
If you don't know how many elements the array will contain, I would use a std::vector instead of a plain array as the vector will grow to accommodate the additional elements.
What you want is the Vector class from the standard template library, it behaves like an array but it will re-size itself if you fill it's internal allocation. If you do not need random access to it (i.e. use the [] opperator) you may want to use the List class instead. If you use List you will need to create an enumerator to step through it.
use std::vector<MyClass>, vector template can be found in <vector> header. YOu must learn a little bit to code well, before coding use any of online available c++ FAQs

C++ Array of Objects

I have an array in a class that should hold some instances of other objects. The header file looks like this:
class Document {
private:
long arraysize;
long count;
Row* rows;
public:
Document();
~Document();
}
Then in the constructor I initialize the array like this:
this->rows = new Row[arraysize];
But for some reason this just sets rows to an instance of Row rather than an array of rows. How would I initialize an array of Row objects?
Both SharpTooth and Wok's answers are correct.
I would add that if you are already struggling at this level you may be better off using a std::vector instead of a built-in array in this case. The vector will handle growing and shrinking transparently.
This should work. One possible "error" would be an incorrect value for arraySize.
However you should better use a std::vector from the standard library for that purpose.
#include <vector>
class Document {
// ...
std::vector<Row> rows;
// ...
};
and in your constructor:
Document::Document() : rows(arraySize) { // ... }
or
Document::Document() { rows.assign(arraySize, Row()); }
If arraySize contains a reasonable value at that point you actually get an array. I guess you trust your debugger and the debugger only shows the 0th element (that's how debuggers treat pointers), so you think there's only one object behind that pointer.
For i in [0;arraysize[, *(this->rows+i) should be an instance of row.
What precisely makes you think that rows is only one element? Make certain that you arraysize isn't 1. If it is, you'll get an array of 1 element. Mind you, you must still call delete [] with an array of size 1.
Also, why is arraysize different than count? Using that terminology, you should be making an array of count elements and arraysize should be equal to sizeof(Row) * count.
Also, you specifically ask "How would I initialize an array of Row objects?". Do you mean allocate? If so, that's how you would do so. If you mean initialize, the default constructor of Row will be called on each element of the array when the array is allocated.

Best Replacement for a Character Array

we have a data structure
struct MyData
{
int length ;
char package[MAX_SIZE];
};
where MAX_SIZE is a fixed value . Now we want to change it so as to support
"unlimited" package length greater than MAX_SIZE . one of the proposed solution
is to replace the static array with a pointer and then dynamically allocating
the size as we require For EX
struct MyData
{
int length ;
char* package;
};
and then
package = (char*)malloc(SOME_RUNTIME_SIZE) ;
Now my question is that is this the most efficient way to cater to the requirement OR is there any other method .. maybe using STL data structures like growable arrays etc etc .
we want a solution where most of the code that works for the static char array should work for the new structure too ..
Much, much better/safer:
struct my_struct
{
std::vector<char>package;
};
To resize it:
my_struct s;
s.package.resize(100);
To look at how big it is:
my_struct s;
int size = s.package.size();
You can even put the functions in the struct to make it nicer:
struct my_struct
{
std::vector<char>package;
void resize(int n) {
package.resize(n);
}
int size() const {
return package.size();
}
};
my_struct s;
s.resize(100);
int z = s.size();
And before you know it, you're writing good code...
using STL data structures like growable arrays
The STL provides you with a host of containers. Unfortunately, the choice depends on your requirements. How often do you add to the container? How many times do you delete? Where do you delete from/add to? Do you need random access? What performance gurantees do you need? Once you have a sufficiently clear idea about such things look up vector, deque, list, set etc.
If you can provide some more detail, we can surely help pick a proper one.
I would also wrap a vector:
// wraps a vector. provides convenience conversion constructors
// and assign functions.
struct bytebuf {
explicit bytebuf(size_t size):c(size) { }
template<size_t size>
bytebuf(char const(&v)[size]) { assign(v); }
template<size_t size>
void assign(char const(&v)[size]) {
c.assign(v, v+size);
}
// provide access to wrapped vector
std::vector<char> & buf() {
return c;
}
private:
std::vector<char> c;
};
int main() {
bytebuf b("data");
process(&b.buf()[0], b.buf().size()); // process 5 byte
std::string str(&b.buf()[0]);
std::cout << str; // outputs "data"
bytebuf c(100);
read(&c.buf()[0], c.buf().size()); // read 100 byte
// ...
}
There is no need to add many more functions to it, i think. You can always get the vector using buf() and operate on it directly. Since a vectors' storage is contiguous, you can use it like a C array, but it is still resizable:
c.buf().resize(42)
The template conversion constructor and assign function allows you to initialize or assign from a C array directly. If you like, you can add more constructors that can initialize from a set of two iterators or a pointer and a length. But i would try keeping the amount of added functionality low, so it keeps being a tight, transparent vector wrapping struct.
If this is C:
Don't cast the return value of malloc().
Use size_t to represent the size of the allocated "package", not int.
If you're using the character array as an array of characters, use a std::vector<char> as that's what vectors are for. If you're using the character array as a string, use a std::string which will store its data in pretty much the same way as a std::vector<char>, but will communicate its purpose more clearly.
Yep, I would use an STL vector for this:
struct
{
std::vector<char> package;
// not sure if you have anything else in here ?
};
but your struct length member just becomes package.size ().
You can index characters in the vector as you would in your original char array (package[index]).
use a deque. sure a vector will work and be fine, but a deque will use fragmented memory and be almost as fast.
How are you using your structure?
Is it like an array or like a string?
I would just typedef one of the C++ containers:
typedef std::string MyData; // or std::vector<char> if that is more appropriate
What you have written can work and is probably the best thing to do if you do not need to resize on the fly. If you find that you need to expand your array, you can run
package = (char*)realloc((void*)package, SOME_RUNTIME_SIZE) ;
You can use an STL vector
include <vector>
std::vector<char> myVec(); //optionally myVec(SOME_RUNTIME_SIZE)
that you can then resize using myVec.resize(newSize) or by using functions such as push_back that add to the vector and automatically resize. The good thing about the vector solution is that it takes away many memory management issues -- if the vector is stack-allocated, its destructor will be called when it goes out of scope and the dynamically-allocated array underlying it will be deleted. However, if you pass the vector around, the data will get copied that can be slow, so you may need to pass pointers to vectors instead.