#include<iostream>
using namespace std;
class test
{
public:
int a,b;
test()
{
cout<<"default construictor";
}
test(int x,int y):a(x),b(y){
cout<<"parmetrized constructor";
}
};
int main()
{
test t;
cout<<t.a;
//t=(2,3);->gives error
t={2,3}; //calls paramterized constructor
cout<<t.a;
}
Run Code Online (Sandbox Code Playgroud)
输出: - 默认constructor4196576parmetrized constructor2
为什么在上面的例子的情况下,参数化的构造函数(即使已经调用了默认构造函数.)在{}而不是在()的情况下被调用.
我有一个下面的程序。我需要理解为什么我们在使用擦除方法时需要递减指针?还有没有更好的方法不会造成这种混乱?
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> myvector{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (auto i = myvector.begin(); i != myvector.end(); ++i) {
if (*i % 2 == 0) {
myvector.erase(i);
i--;// why do we need to decrement ?
}
}
// Printing the vector
for (auto it = myvector.begin(); it != myvector.end(); ++it)
cout << ' ' << *it;
return 0;
}
Run Code Online (Sandbox Code Playgroud)