关于功能,我有一点点.我相信这可能是因为我没有使用它们.我的代码如下:
/*date difference calculator*/
#include <stdio.h>
int main()
{
int Date1 = 0, Date2 = 0, Dif, F, L, D1, D2, M1, M2, Y1, Y2;
int x[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int y[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char A1, A2, B1, B2;
/*input first date*/
fprintf (stderr , "Enter first date,in the form <day>/<month>/<year> or <day>-<month>-<year>.\nWhere <day>,<month> and <year> are …Run Code Online (Sandbox Code Playgroud) 我目前正在查看STL库,我想知道为什么vector<string> names;我必须调用一个向量类remove();,如下所示:
names.erase(remove(names.begin(), names.end(), "Simon"), names.end());
Run Code Online (Sandbox Code Playgroud)
在使用列表类时,list<string> names;我可以调用该函数,如下所示:
remove("Simon");
Run Code Online (Sandbox Code Playgroud)
我也注意到同样为reverse();为 vector<string> names;它被称为如下:
reverse(names.begin(), names.end());
Run Code Online (Sandbox Code Playgroud)
虽然list<string> names;它被称为如下:
names.reverse();
Run Code Online (Sandbox Code Playgroud)
总是调用矢量的方式更合适吗?为什么是这样?我对C++很陌生,所以我很想知道最好的做事方式.
我有以下代码:
StringC StringC::operator+(const StringC& other) const
{
strcat(ps,other.ps);
return ps;
};
Run Code Online (Sandbox Code Playgroud)
相关标题如下:
class StringC {
private:
char* ps;
public:
StringC(char const *);
StringC& operator=(const StringC&);
StringC operator+(const StringC& other) const;
void Print();
Run Code Online (Sandbox Code Playgroud)
我的理解是,const在operator+函数中的使用应该阻止我能够改变ps,other但是我仍然能够改变它们.
我尝试过以下方法:
void Class1::Method1() const
{
Variable = Variable + 1;
};
void Class1::Method1(const int v)
{
v = 0;
Variable = v + 1;
};
Run Code Online (Sandbox Code Playgroud)
并且这个错误如预期的那样,我假设我在使用const时遗漏了一些东西,但是在使用时会产生错误的第一个代码strcat?