Autoinitializing values in an array [theory]
Page 1 of 113 Replies - 422 Views - Last Post: 14 November 2011 - 12:53 PM
#1
Autoinitializing values in an array [theory]
Posted 11 November 2011 - 12:55 PM
Like
int a[] = 0;
or something similar?
Replies To: Autoinitializing values in an array [theory]
#2
Re: Autoinitializing values in an array [theory]
Posted 11 November 2011 - 01:00 PM
int a[100] = {0};
then the compiler should initialize the entire array to 0. If you do this:
int a[100] = {99};
then the compiler should only set the first index a[0] to 99.
This post has been edited by CTphpnwb: 11 November 2011 - 01:00 PM
#3
Re: Autoinitializing values in an array [theory]
Posted 11 November 2011 - 01:02 PM
EDIT: I accidentally a word.
This post has been edited by NeoTifa: 11 November 2011 - 01:12 PM
#4
Re: Autoinitializing values in an array [theory]
Posted 12 November 2011 - 03:17 PM
int table[];
then go back and be like
table[size] = {EMPTY};
where EMPTY is already defined (-1 if you feel so inclined)
??????????
#5
Re: Autoinitializing values in an array [theory]
Posted 12 November 2011 - 03:33 PM
#6
Re: Autoinitializing values in an array [theory]
Posted 12 November 2011 - 04:19 PM
#7
Re: Autoinitializing values in an array [theory]
Posted 12 November 2011 - 04:28 PM
int example [2]={1,2};
but you cant do this
int example[2];
example[2] ={1,2};
you have to specify the size of the array while declaring it or you will have to do dynamically allocate as CTphpnwb said like this
int table[]; table= new int [size];
or use vectors
#include <vector> std::vector <int> table ; table.push_back(1);
#8
Re: Autoinitializing values in an array [theory]
Posted 12 November 2011 - 04:50 PM
ClosedHash::table = new int[size];
Never mind again, I just made table a pointer.
int* table;
...
table = new int[size];
This post has been edited by NeoTifa: 12 November 2011 - 05:32 PM
#9
Re: Autoinitializing values in an array [theory]
Posted 13 November 2011 - 05:22 AM
NeoTifa, on 12 November 2011 - 07:19 PM, said:
You can treat vectors like arrays. For example, this could be a vector or an array:
table[some_index] = some_value;
There's no way to tell with out seeing the declaration.
It's easier to initialize a vector with nonzero values:
using namespace std;
#include <iostream>
#include <vector>
int main () {
vector<int> myvec;
myvec.assign(25,110);
cout << myvec[10] << endl;
return 0;
}
NeoTifa, on 12 November 2011 - 07:50 PM, said:
...
table = new int[size];
That's dynamic allocation. Be sure to delete the array when you're done with it or you will have a memory leak.
#10
Re: Autoinitializing values in an array [theory]
Posted 13 November 2011 - 05:42 PM
#11
Re: Autoinitializing values in an array [theory]
Posted 13 November 2011 - 05:52 PM
template<class T>
struct Array {
T *data;
const unsigned int size;
Array(unsigned int n) : size(n) { data = new T[size]; }
~Array() { delete [] data; }
T &operator[](unsigned int index) { return data[index]; }
};
#12
Re: Autoinitializing values in an array [theory]
Posted 13 November 2011 - 06:55 PM
#include <algorithm>
template<class T>
struct Array {
T *data;
unsigned int size;
Array(const unsigned int n) : size(n) { data = new T[size]; }
Array(const Array& other) {
size = other.size;
data = new T[size];
std::copy(other.data, other.data+size, data);
}
~Array() { delete [] data; }
T &operator[](unsigned int index) { return data[index]; }
Array<T>& operator=(const Array& other) {
if (&other != this) {
delete[] data;
size = other.size;
data = new T[size];
std::copy(other.data, other.data+size, data);
}
return *this;
}
};
#include <iostream>
#include <string>
using namespace std;
int main() {
Array<string> tempNames(10);
tempNames[0] = "Nicolas Sarkozy";
tempNames[1]= "Francois Fillon";
tempNames[2] = "Bruno Le Maire";
tempNames[3] = "Nadine Morano";
tempNames[4] = "Valerie Pecresse";
Array<string> frenchGovNames(tempNames);
Array<string> namesToDisplay(10);
namesToDisplay = frenchGovNames;
for(int i = 0; i < 10; i++) {
cout << "Name: \"" << namesToDisplay[i] << "\"" << endl;
}
return 0;
}
#13
Re: Autoinitializing values in an array [theory]
Posted 14 November 2011 - 07:19 AM
You're right, of course, I should have added:
private: Array(const Array&);
So no one would be doing silly stuff like:
Array<string> foo(bar); Array<string> baz = bar;
Note, this is also the reason for the const, to frustrate misuse. Why? Because arrays aren't that clever either.
If we're going for clever, then it's time to have a little privacy:
template<typename T>
class Array {
public:
Array(const Array &);
Array(unsigned int);
~Array();
Array<T>& operator=(const Array &);
T &operator[](unsigned int index);
const T &operator[](unsigned int index) const;
unsigned int length() const;
private:
T *data;
unsigned int size;
};
You got me to thinking though. Do I want my operator= to simply make a copy, or refer to the same instance? The issue then becomes shared memory space and how to manage it.
So, I thought this was fun:
#include <iostream>
template<typename T>
class Array {
public:
Array(const Array &);
Array(unsigned int);
~Array();
Array<T>& operator=(const Array &);
T &operator[](unsigned int index);
const T &operator[](unsigned int index) const;
unsigned int length() const;
Array<T> clone() const;
private:
struct Instance {
T *data;
const unsigned int size;
unsigned int count;
Instance(unsigned int n) : size(n), data(new T[n]), count(1) { }
} *instance;
};
void show(const Array<std::string> &a) {
for(unsigned int i=0, size=a.length(); i<size; i++) {
std::cout << "Name: \"" << a[i] << "\"" << std::endl;
}
std::cout << std::endl;
}
int main() {
using namespace std;
Array<string> tempNames(10);
tempNames[0] = "Nicolas Sarkozy";
tempNames[1]= "Francois Fillon";
tempNames[2] = "Bruno Le Maire";
tempNames[3] = "Nadine Morano";
tempNames[4] = "Valerie Pecresse";
Array<string> frenchGovNames(tempNames);
Array<string> namesToDisplay(10);
namesToDisplay = frenchGovNames;
Array<string> ac = namesToDisplay.clone();
show(namesToDisplay);
tempNames[3] = "Bob";
show(namesToDisplay);
show(ac);
return 0;
}
template<typename T> Array<T>::Array(unsigned int n) : instance(new Instance(n)) { }
template<typename T> Array<T>::Array(const Array &a) : instance(a.instance) { instance->count++; }
template<typename T> Array<T>& Array<T>::operator=(const Array &a) {
if (instance!=a.instance) {
if (--(instance->count)==0) { delete instance; }
instance = a.instance;
instance->count++;
}
return *this;
}
template<typename T> Array<T>::~Array() { if (--(instance->count)==0) { delete instance; } }
template<typename T> T &Array<T>::operator[](unsigned int index) { return instance->data[index]; }
template<typename T> const T &Array<T>::operator[](unsigned int index) const { return instance->data[index]; }
template<typename T> unsigned int Array<T>::length() const { return instance->size; }
template<typename T> Array<T> Array<T>::clone() const {
Array<T> a(instance->size);
std::copy(instance->data, instance->data+instance->size, a.instance->data);
return a;
}
Probably not the most C++y way to go and far from thread safe, but it amused me.
#14
Re: Autoinitializing values in an array [theory]
Posted 14 November 2011 - 12:53 PM
I like how people keep posting on a solved topic instead of helping me with my current problem. Lol. Oh well, too late now.
This post has been edited by NeoTifa: 14 November 2011 - 12:53 PM
|
|

New Topic/Question
Reply







MultiQuote







|