better cpp vector insert(initial) writing [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 days ago.
Improve this question
i am trying to initial a vector of a defined class in another instance, the test code is as:
#include <vector>
class A
{
public:
float x = 0.0;
};
class B
{
public:
std::vector<A> b;
};
int main()
{
B x;
for(int i = 0; i < 3; i++)
{
A y;
x.b.insert(x.b.begin(), y);
}
}
but is there a more brilliant writing for the initial of x?
something maybe like
x.b.insert(x.b.begin(), 3, A);
which i got error error: expected primary-expression before ‘)’ token
thx
edit: sorry, edited for a better example

No need for a loop or insert, just construct the vector and assign it
B.x = std::vector<A>(3);
or if you have a particular value in mind
A y = something;
B.x = std::vector<A>(3, y);
Maybe this code should be in the constructor for B.

Related

How can I access member variables of a vector of objects that is a member variable from a class? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I do know how to access member variables given a vector of objects but suppose
if I have a class called "layer" that is
class layer{
public:
layer(.... that initializes "val" .... );
vector<vector<double>> getval(){return val;}
private:
vector<vector<double>> val;
}
and then suppose there is another class that is
class Net{
public:
Net( ..... that initializes "nn" ..... );
vector<layer> getnn(){ return nn; }
private:
vector<layer> nn;
}
So in the main function, I could create an object like
Net n( ....... )
and in the main function I could get vector of objects via
n.getnn();
but the question is how could I get the specific, given i index,
vector<vector<double>> val
at nn[i]
float value = n.getnn()[i].getval()[j][k];

C++: struct accessing instance of itself [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have come across a rather unusual issue in my code. A struct needs to be able to access instances of itself.
Relavent portion of code:
struct crtr {
char f;
void foo() {
for(int i=0; i<creatures[f].size(); i++) {/*code that accesses creatures[f][i]*/}
}
};
vector<crtr> creatures[10];
Of course, this is nowhere close to working - crtr.foo() requires creatures, while creatures requires crtr. Is there some way to initialize creatures before crtr, perhaps changing the vectors' data type? (preferably with minimal pointers, if possible)
I must be missing something, what's wrong with this?
struct crtr {
char f;
void foo();
};
vector<crtr> creatures[10];
void crtr::foo() {
for(int i=0; i<creatures[f].size(); i++) {/*code that accesses creatures[f][i]*/}
}
Also crtr::foo can be inline if that's required.
Use forward declaration of the struct:
struct crtr;
vector<crtr> creatures[10];
struct crtr {
char f;
void foo() {
for(int i=0; i<creatures[f].size(); i++) {/*code that accesses creatures[f][i]*/}
}
};

Use of lambda during member variable initialization within class declaration [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
For readability, I like to initial member variables within class declaration. I am also intrigued by used of lambda during such initialization ( see snippet). My question is what are the advantages or disadvantages of using such lambda functions in the declaration over member functions.
class foo
{
private:
int index_ = 5;
int value_ = [](int index)
{
int result = 0;
for( int i = 0;i <index; i++)
result += i;
return result;
}(index_ );
};
Edited:
As bar::bar(int index) :index_(index) {} is more optimized than bar::bar(int index) {index_ = index;}, whether the above code performs better or it just the readability.

Why this class doesn't make error? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
#include <iostream>
using namespace std;
Class ITEM{
private:
int cnt;
public:
ITEM(){}
void func(ITEM a){
a.cnt = 10;
}
};
int main(){
return 0;
}
I assume that red line will make the error.
because 'a.cnt' value is private value.
I learned that private value must be modified inner of class.
void func(A a){
a.cnt = 10; //valid
}
notice that function is inside the class, so it becomes it member and class member functions have access to private members.
Note that it is being modified inner of the class ITEM using a member function,which is perfectly valid

Issue of the default parameter in class [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a question about default parameter in C++ class:
#include <stdio.h>
class Test {
public :
Test(int ai = 0, int bi = 0)
: a(ai), b(bi) { }
void print() { printf("a = %d, b = %d", a, b); };
private :
int a, b;
};
int main() {
Test t(1);
t.print();
return 0;
}
In this code,
result will be "a = 1 , b = 0(default parameter)"
If I want to set "a" is default parameter and "b" is other value, how should I write the code?
Is there any way to solve this question?
In C++, you may omit function arguments that are declared with default parameters, but you can only omit one or more function arguments from the end of the argument list. So, given
Test(int ai = 0, int bi = 0)
there is no way to pass a value for bi and leave ai as the default value.