#include <iostream>
#include <vector>
class TableauPseudoDynamique {
private:
int* data;
size_t size;
public:
// Constructor to initialize an empty array
TableauPseudoDynamique() : data(nullptr), size(0) {}
// Destructor to free the allocated memory
~TableauPseudoDynamique() {
delete[] data;
}
// Function to add an element to the end of the array
void ajouterElement(int element) {
int* newData = new int[size + 1];
for (size_t i = 0; i < size; ++i) {
newData[i] = data[i];
}
newData[size] = element;
delete[] data;
data = newData;
size += 1;
}
// Function to access an element at a specific index
int getElement(size_t index) const {
if (index < size) {
return data[index];
} else {
// Handle out-of-bounds access
std::cerr << "Index out of bounds." << std::endl;
return -1; // Placeholder value
}
}
// Function to display the elements of the array
void afficherTableau() const {
std::cout << "[";
for (size_t i = 0; i < size; ++i) {
std::cout << data[i];
if (i < size - 1) {
std::cout << ", ";
}
}
std::cout << "]" << std::endl;
}
};
int main() {
// Create an instance of TableauPseudoDynamique
TableauPseudoDynamique tableau;
// Add elements to the array
tableau.ajouterElement(10);
tableau.ajouterElement(20);
tableau.ajouterElement(30);
// Display the elements of the array
tableau.afficherTableau();
// Access and display an element at a specific index
size_t indexToAccess = 1;
std::cout << "Element at index " << indexToAccess << ": " << tableau.getElement(indexToAccess) << std::endl;
return 0;
}