Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize empty vector in structure - c++

Tags:

c++

vector

I have a struct:

typedef struct user {     string username;     vector<unsigned char> userpassword; } user_t; 

I need to initialize userpassword with an empty vector:

struct user r={"",?}; 

What should I put instead of ??

like image 507
Tomasz Avatar asked Nov 17 '12 21:11

Tomasz


People also ask

How do you initialize a vector in a struct?

To properly initialize a structure, you should write a ctor to replace the compiler provided ctor (which generally does nothing). Something like the following (with just a few attributes): struct grupo { float transX, transY; // ...

Can you make a vector of a struct?

You can actually create a vector of structs!


2 Answers

Both std::string and std::vector<T> have constructors initializing the object to be empty. You could use std::vector<unsigned char>() but I'd remove the initializer.

like image 105
Dietmar Kühl Avatar answered Sep 19 '22 09:09

Dietmar Kühl


Like this:

#include <string> #include <vector>  struct user {     std::string username;     std::vector<unsigned char> userpassword; };  int main() {     user r;   // r.username is "" and r.userpassword is empty     // ... } 
like image 28
Kerrek SB Avatar answered Sep 21 '22 09:09

Kerrek SB