我只是测试git,看看我是否可以将它用于我的工作.我遇到了一个似乎很小的问题,但可能会成为真正的代码.我的文件看起来像:text.txt 1 2 3 4我有一个本地分支"branch1"并在分支和主服务器中提交更改.在主人我改变了第二行的第一行.所以diff for master看起来像这样:
+1 master
2
3
4
Run Code Online (Sandbox Code Playgroud)
对于分支它是:
1
-2
+2b1
3
4
Run Code Online (Sandbox Code Playgroud)
运行git merge branch1解决冲突:
<<<<<<< HEAD
1 master
2
=======
1
2b1
>>>>>>> branch1
3
4
Run Code Online (Sandbox Code Playgroud)
我知道这个可以轻松解决.但无论如何,这又是一场冲突.不应该git能合并吗?
在boost :: scoped_ptr中operator*,operator->它们是声明的const函数,尽管它们返回T&并且T*可能允许客户端更改底层数据.这违反了逻辑常量的概念(Myers,Effective C++)
const函数不应该有签名吗?
const T& operator*() const;
const T* operator->() const;
Run Code Online (Sandbox Code Playgroud) 我正在创建一个"连接迭代器",即一个迭代器,它将遍历ints中的s int**.
它的构造函数需要:
T**,表示每个子数组的开头.T**,表示每个子数组的结尾.瞧,我遇到了一个goto似乎合适的情况.
但是我内心的某些东西尖叫着"不!" 所以我以为我会来这里问:
goto这种情况吗?(如果我这样做,它会提高可读性吗?)#include <algorithm>
template<class T>
class lazy_concat_iterator
{
// This code was meant to work for any valid input iterator
// but for easier reading, I'll assume the type is: T**
mutable T** m_endIt; // points to an array of end-pointers
mutable T** m_it; // points to an array of begin-pointers
mutable bool m_started; // have we started iterating?
mutable T* …Run Code Online (Sandbox Code Playgroud) 这两个函数原型有什么区别?
void apply1(double(f)(double));
void apply2(double(*f)(double));
Run Code Online (Sandbox Code Playgroud)
如果目标是将提供的函数应用于数组,那么与其他版本相比,版本是否更快?
编辑:实施的一个例子:
#include <iostream>
#include <vector>
#include <cmath>
// First version
template<typename Type> void apply1(std::vector<Type>& v, Type(f)(Type))
{
for (unsigned int i = 0; i < v.size(); ++i) {
v[i] = f(v[i]);
}
}
// Second version
template<typename Type> void apply2(std::vector<Type>& v, Type(*f)(Type))
{
for (unsigned int i = 0; i < v.size(); ++i) {
v[i] = f(v[i]);
}
}
// Main
int main()
{
std::vector<double> v = {1., 2., 3., 4., 5.};
apply1(v, std::sin); …Run Code Online (Sandbox Code Playgroud) 模板类和普通类:
template <typename Type>
class Holder
{
public:
Holder(const Type& value) : held_(value)
{
cout << "Holder(const Type& value)" << endl;
}
Type& Ref() { return held_; }
private:
Type held_;
};
class Animal
{
public:
Animal(const Animal& rhs) { cout << "Animal(const Animal& rhs)" << endl; }
Animal() { cout << "Animal()" << endl; }
~Animal() { cout << "~Animal" << endl; }
void Print() const { cout << "Animal::Print()" << endl; }
};
Run Code Online (Sandbox Code Playgroud)
然后我想Holder<Animal>用这个语句实例化一个Holder<Animal> a(Animal()); …
我想在VS2008下编译beecrypt库.但是以下几种结构会产生语法错误(C2059语法错误:'.'):
const hashFunction md5 = {
.name = "MD5",
.paramsize = sizeof(md5Param),
.blocksize = 64,
.digestsize = 16,
.reset = (hashFunctionReset) md5Reset,
.update = (hashFunctionUpdate) md5Update,
.digest = (hashFunctionDigest) md5Digest
};
Run Code Online (Sandbox Code Playgroud)
VC++不接受开头的点.如果我评论上面的内容,我稍后会收到链接错误(LNK2001未解析的符号_md5) - 所以我想它必须取消注释.
这个结构是什么?我需要什么?我如何告诉VS2008编译它?
我经常使用引用来简化代码的外观:
vec3f& vertex = _vertices[index];
// Calculate the vertex position
vertex[0] = startx + col * colWidth;
vertex[1] = starty + row * rowWidth;
vertex[2] = 0.0f;
Run Code Online (Sandbox Code Playgroud)
编译器会识别并优化它,因此它基本上如下吗?
_vertices[index][0] = startx + col * colWidth;
_vertices[index][1] = starty + row * rowWidth;
_vertices[index][2] = 0.0f;
Run Code Online (Sandbox Code Playgroud) 以下是一个代码段
#define T 0xFF
using namespace std;
int main(void) {
char c = T;
bool *pc = (bool *)(&c);
bool nc = !(*pc);
cout << "print: " << hex << nc << endl;
nc = T;
cout << "print: " << hex << nc << endl;
nc = c;
cout << "print: " << hex << nc << endl;
}
Run Code Online (Sandbox Code Playgroud)
结果是
print: fe
print: 1
print: 1
Run Code Online (Sandbox Code Playgroud)
如果使用值0xFF将char转换为bool,则bool值为1.
但是当类型转换为指向bool指针的char指针时,0xFF变为0xFE,只有最后一位被翻转!操作.
似乎gcc假定bool为0或1并且如果没有调用bool对象的构造函数,它将只解释内存以包含bool并翻转最低有效位.
但是当bool由char设置时,是否会触发复制构造函数?但为什么不同?
我是新的矢量.我正在尝试将对象添加到矢量中.但程序无法编译,因为我的代码有问题.但我不知道它是什么.错误是:
error C2664: 'void std::vector<_Ty>::push_back(_Ty &&)' : cannot convert parameter 1 from 'Line (void)' to 'Line &&'
Run Code Online (Sandbox Code Playgroud)
代码是:
Line help_line ();
cin >> ln_quan;
vector <Line> figure_line;
for (int i = 0 ; i < ln_quan ; i++)
{
figure_line.push_back(help_line);
}
Run Code Online (Sandbox Code Playgroud)
编译器说错误位于第6行(figure_line.push_back(help_line);).
我放弃了试图找到一个解释如何添加对象的教程(在做这些事情时我很容易放弃......).
'Line(void)'和'Line &&'是什么意思?'Line(void)'是'Line'类吗?如果是这样,在这种情况下'(void)'是什么意思?
int main()
{
const int* x;
int* pa = x;//removes const, so UB.
const int*& pb = pa;//error
int* pd = pb;//error
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道pointer to non-const data用a 定义a 是不可能的pointer to const data,因为它会自动取消constness,允许我修改值.
但是第二次初始化有什么问题?我知道引用是某个东西的别名以及它是如何工作的,但仍然没有得到实际发生的事情.我想第二个错误的解释将有希望启发我第三个错误.
任何人都能解释一下吗?谢谢!