我正在编写一个递归函数,它接受一个char数组,它表示一个数字,以及一个指向该数组中数字的指针.该函数的要点是像++运算符一样递增数字.但是,当我尝试使用数字'819'时.它不会将其增加到'820',而是将其更改为'810'(它会增加最后一个数字,但不会执行我想要的递归).有人可以帮我解决这个问题吗?谢谢.
#include <stdio.h>
char* inc(char *num, char* p)
{
if( *p>='0' && *p<='8' )
{
*p++;
}
else if ( *p=='9' )
{
*p = '0';
inc(num, --p);
}
return num;
}
main()
{
char x[] = "819";
printf("%s\n", inc(x, x+strlen(x)-1) ); //pass the number and a pointer to the last digit
}
Run Code Online (Sandbox Code Playgroud) 我想开发一个类似于薪水计算的系统.薪水有基本价值.最重要的是,员工可以获得一些奖金或惩罚.装饰模式似乎适合这种情况
Salary finalSalary = new HardworkBonus(new LateComingPenalty(new BasicSalary()))
System.out.println("Your total salary is : "+ finalSalary.calculate())
Run Code Online (Sandbox Code Playgroud)
另外,我想保存每个计算的结果.所以最后,即使经过计算,我也可以检索LateComingPenalty的数量.
这似乎是一个常见的问题,涉及这种发票计算.可能有一些比装饰模式更好的选择.你有更好的建议吗?
我创建了一个带有以下标志的命名管道:
我从服务器端调用ConnectNamedPipe并等待客户端连接.
从客户端我调用CallNamedPipe连接到服务器并写入长度为N的数据.
在服务器端:
问题:
我想引入自定义标头来指示消息本身的缓冲区长度,但这听起来很多变化.
有没有更好更可靠的方法来获取从管道读取数据的长度?
考虑以下两个例子.
class ClassOne
{
//class definition is here
};
std::vector< ClassOne > myListOfObjects;
std::vector< ClassOne >::const_iterator iter = myListOfObjects.begin();
Example 1:
for( ; iter < myListOfObjects.end(); **++iter**)
{
//some operations
}
OR
Example 2:
for( ; iter < myListOfObjects.end(); **iter++**)
{
//some operations
}
Run Code Online (Sandbox Code Playgroud)
哪一个更快?循环上下文中的++ iter或iter ++.
从Brian的帖子复制(使问题更简洁).
我在接受采访时被问及在C++中使用带有类声明的virtual关键字的用法是什么,我回答说虚拟关键字不能与C++中的类声明一起使用.面试官说这是可能的,并让我稍后进行测试.
现在我已经自己检查了一下,我已经知道这是可能的,这不是编译器错误.事实上,当我使用Visual C++编译器执行类似的操作时:
virtual class Test
{
int i;
};
Run Code Online (Sandbox Code Playgroud)
我得到一个编译器警告"警告C4091:'virtual':当没有声明变量时,在'Test'的左边忽略".我还没有发现这个警告意味着什么,以及虚拟关键字的用法是什么.如果没有有用的用法,那么为什么首先允许这样做,为什么这不是编译器错误.
可能重复:
测试有效性指针(C/C++)
是否有任何简单的Win32 API来检查指针在用于写入操作之前是否指向有效的内存位置?
目前,我一直在以编程方式从二进制数据文件中读取数据列表,如下所示:
tplR = (double*) malloc(sampleDim[0]*sizeof(double));
printf("tplR = %d\n", fread(tplR, sizeof(double), sampleDim[0], dfile));
但是,由于我想find_if()在这些列表上使用函数,我需要将tplR转换为stl中的列表类型.就一般的C++编程实践而言,通常只有在我必须时才将tplR放入列表中才是好的做法?
如果我确实创建了另一个成员变量,例如tplRList,那么将所有sampleDim [0]个双精度条目数从tplR推送到tplRList的最简单方法是什么?逐个推送它们直到增量计数器等于sampleDim [0]?
提前致谢.
我究竟做错了什么?
#include <iostream>
#include <deque>
using namespace std;
struct mystruct {
int number1;
int number2;
};
int main() {
std::deque<mystruct> mydeque;
mydeque.number1.push_front(77);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 假设我有两个归类Base和Derived,即:
#include <iostream>
class Base {
public:
Base ()
: m_name("Base")
{
}
virtual ~Base ()
{
}
virtual void method (std::ostream & out) const
{
out << m_name << std::endl;
}
protected:
Base (std::string name)
: m_name(name)
{
}
private:
std::string m_name;
};
class Derived : public Base {
public:
Derived ()
: Base("Derived")
{
}
virtual ~Derived ()
{
}
virtual void method (std::ostream & out) const
{
static_cast<const Base * const>(this)->method(out);
}
}; …Run Code Online (Sandbox Code Playgroud)