constructor of a vector of structures

Hello

I am a little bit swimming right now.
I try to get names and ages of my contacts.
I creates a structure for these informations, and would like to put them in a vector. However, the constructor for this one that contains: names, ages, and size of the vector isn't obvious right now !!! here is where I got to till now :
CPP file for the functions ---> ItVect.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "ItVect.h"

//Constructors
ItVect::ItVect():numElements(0), capacity(10), info(new infoContact){}

ItVect::ItVect(numElements n, info{String name, int age}):numElements(n), capacity(n), info(new infoContact[]){
	for(int i=0; i<n; i++){
		info[i].name;
		info[i].age;
	}
}
//Destructor	
ItVect::~ItVect(){
	delete [] info;
}

My Header ---> ItVect.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef ITVECT_H
#define ITVECT_H

using namespace std;

struct infoContact{
	string name;
	int age;
};

class It{
	public:
		ItVect();
		ItVect(int n, info{string name="Empty"; int age=0});
                ~ItVect();
	private:
		infoContact *info;
		int numElements;
		int capacity;
}
#endif 

I just started it. so, if you have anything else to say about it, I m ready to receive it.
You want to create your own container type that manages its dynamic memory as a "learning experience"?

I'm not familiar with the info{string name="Empty"; int age=0} syntax for a function argument. Is it something new?


You do have members numElements and capacity. What logical concepts do they represent?
numElements represent the number of contacts that are in the vector
Capacity represents the size of the vector.(considering that it will get bigger after adding more contacts)

For the syntax
info{string name="Empty"; int age=0}
Ito represents my structure, and the rest is the informations in my structure :
(This could represents my vector of structure . That s what i want to do)
_____________________________________________________________
|name1, age1 |name2, age2 |name3, age3 |....... ..|name n, age n
|___________|___________|___________|______|__________________
numElements represent the number of contacts that are in the vector
Capacity represents the size of the vector.(considering that it will get bigger after adding more contacts)


So you need to create your own vector? You might want to PM me.
Last edited on
What do you mean by private message? all the problem is on the board already !
When you have constructor, consider adding push_back() function and insert() function.

And also operator[] function.
That might be a lot. But let we know if you can do it.
Lets rephrase:
For the syntax
info{string name="Empty"; int age=0}
Ito represents my structure, and the rest is the informations in my structure :
Where in the C++ Standard is this syntax described?


The other part was ok:
numElements: how many contactInfo objects are stored in the Vector
capacity: how many contactInfo objects can be stored in the Vector

The capacity is an implementation detail. It allows certain optimizations. An icing on a cake, but currently ...

Your current Vector can be created as empty, or as large enough to store initializer data. There seems to be no way to access the elements, nor to change numElements, so capacity is moot for the moment.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Foo {
  T * array = nullptr;
  size_t size = 0;
public:
  Foo() = default;
  Foo( T * data, size_t count )
    : array( new T [count] ), size( count )
  {
    for ( size_t x = 0 ; x < size; ++x ) array[x] = data[x];
  }

  // Some of these must be written too:
  // copy constructor
  // copy assignment
  // move constructor
  // move assignment
  // destructor
};

int main() {
  T something[] = /* initializer*/;
  auto N = sizeof(something) / sizeof(T);
  Foo bar( something, N );
  return 0;
}
Last edited on
Thank you.

Does it mean I don't have to declare any "struct" to get all my contact's data together?

Well, my example has fictional type T, but you will have infoContact instead.
Thank you all for your help !
i figured it out !
it was really helpful
thank you
Topic archived. No new replies allowed.