Is Std::Vector<T> a 'User-Defined Type'

Is std::vector<T> a `user-defined type`?

When Clause 17 says "user-defined" it means "a type not defined in the standard" so std::vector<int> is not user-defined, neither is std::string, so you cannot specialize std::vector<int> or std::vector<std::string>. On the other hand, struct MyClass is user-defined, because it's not a type defined in the standard, so you can specialize std::vector<MyClass>.

This is not the same meaning of "user-defined" used in clauses 1-16, and that difference is confusing and silly. There is a defect report for this, with some discussion recorded that basically says "yes, the library uses the wrong term, but we don't have a better one".

So the answer to your question is "it depends". If you're talking to a C++ compiler implementor or a core language expert, std::vector<int> is definitely a user-defined type, but if you're talking to a standard library implementor, it is not. More precisely, it's not user-defined for the purposes of 17,6.4.2.1.

One way to look at it is that the standard library is "user code" as far as the core language is concerned. But the standard library has a different idea of "users" and considers itself to be part of the implementation, and only things that aren't part of the library are "user-defined".

Edit: I have proposed changing the library Clauses to use a new term, "program-defined", which means something defined in your program (as opposed to UDTs defined in the standard, such as std::string).

User Defined types and std::vector in C++

User-defined types need good copying semantics always. This is true even when you are not putting them into vectors. What it means to copy one object to another varies, but clearly one requirement is that doing so shouldn't crash the program. So the real question is whether your user defined type has good copying semantics or not. Obviously without seeing the type it is hard to say.

If you have a simple type like struct Point { int x, y; }; then that already has good copying semantics and you don't need to do anything more. Same applies if your class contains other objects that themselves have good copying semantics, so there's no problem if you wanted to include a std::string in your type, e.g. struct NamedPoint { std::string name; int x, y; };

Usually the problem arises when the class has to do something in the destructor such as delete some memory. Then you need to write an assignment operator and copy constructor.

Much more detail can be found here.

PS that discussion you linked was somewhat confused.

Printing std::vector of a user defined type

The issue is std::copy can't find operator<<, without the help of ADL. It knows nothing about operator<< defined by you.

So you need to move operator<< into the same namespace where class A declared, to make ADL to take effect, then operator<< could be found.

namespace N { 
class A;
std::ostream & operator<<(std::ostream & out, const A & a);
}

class N::A{};

std::ostream & N::operator<<(std::ostream & out, const N::A & a)
{
out << "A";
return out;
}

LIVE

Note for the first case, it works well, still because of ADL. Just the namesapce is global namespace (where class A and operator<< defined).

How to define a conversion from a standard type to a user defined type?

If I understand your issue correctly, what you need is another constructor for Node, something like this

Node(const std::pair<char, int>& pair) : character{pair.first}, 
frequency{pair.second} { }

Note that I am using the member initializer list to initialize the data of your class. This is the recommended way to do so, see for more info https://en.cppreference.com/w/cpp/language/initializer_list

Going forward you might want to mark the constructor as explicit to avoid unwanted conversions (see What does the explicit keyword mean?) and use emplace_backinstead of push_back to avoid the construction of a temporary https://en.cppreference.com/w/cpp/container/vector/emplace_back. The marking of the constructor to explicit might not fit your use case, the choice is yours. If you mark the constructor explicit the push_back as written now will stop working, you will need to create explicitly a Node or use emplace_back.

C++ Input data to vector that is of a user-defined type?

The problem is in your case statements ...

  switch ( inputcode )
{
case 1:// Add Account
{
BankingSystem add;
add.accAdd();
}
break;

case 2:// Delete Account
{
BankingSystem del;
del.accDelete();
}
break;

case 3:// Account Inquiry
{
BankingSystem inquire;
inquire.accInquiry();
}
break;

case 4: // Save Accounts to File
{
BankingSystem save;
save.accSave();
}
break;

case 5: // Load Accounts from File
{
BankingSystem load;
load.accLoad();
}
break;

You are creating and using a new instance of your BankingSystem class in each case statement, rather than using the one you created at the beginning of your main() function. That is why you always get a size() of 1. You want to use your myBankingSystem pointer instead ...

  switch ( inputcode )
{
case 1:// Add Account
myBankingSystem->accAdd();
break;

case 2:// Delete Account
myBankingSystem->accDelete();
break;

case 3:// Account Inquiry
myBankingSystem->accInquiry();
break;

case 4: // Save Accounts to File
myBankingSystem->accSave();
break;

case 5: // Load Accounts from File
myBankingSystem->accLoad();
break;

i am trying to sort a vector of certain user defined data type but it is giving me syntax error c++

There are several things wrong.
First, This line is really saying vector<train>* arr, Pointer to a vector. But you used vector so array and memory management is totally internal to it. And you are using sort which is in-place sorting algorithms means, it modifies the original array, so you have to pass it via reference.

int maxTrains(vector<train> arr[])

This is the correct way.

int maxTrains(vector<train>& arr) //if you want modify original

Second, are you using sort which expects sort(Iterator begin, Iterator end, compare); This is how you call std::sort.

sort(arr.begin(), arr.end(),compare);

This is what you want, the rest works fine. I do not know what exactly you want to do with maxTrain so I am just fixing the errors.

 int maxTrains(vector<train>& arr)
{
int ans=0;
sort(arr.begin(), arr.end(),compare);
return ans;
}

Using these lines is a very bad practice.

#include"bits/stdc++.h"
using namespace std;

Try to be explicitly as much as you can, this way you can learn more.

vector of user defined type

The problem is that you are trying to create a vector of references. The object's type to be stored in the vector must be assignable, which is not the case for references. A reference can only be initialized upon declaration and can not be changed later.

What you most probably want is

Test t(31415);
std::vector<Test> vet;
vet.push_back(t);

which creates a copy of t that is then stored in the vector.

You can actually see the problem in the compiler error messages, although they are quite cryptic. The compiler fails to generate code for a *allocator<Test&>, which takes care of memory allocation of the objects to be stored in the vector - there is no way to allocate memory for a reference.



Related Topics



Leave a reply



Submit