我必须0
在字符串中用(整数0)替换空格.即null以终止此字符串中的每个单词.
char data[] = "r1 2 3 1.0kohm \n v1 1 0 5.5v";
Run Code Online (Sandbox Code Playgroud)
当我喜欢这样的时候:
int index = 0;
char token[50];
while (data[index] != '\0')
{
token[index] = 0;
index++;
}
Run Code Online (Sandbox Code Playgroud)
但它取代了字符0而不是整数0.
我正在使用三维数组.我这样声明:
int (*DoubleStride_StateTable)[255][255] = new int[StateTable_length][255][255];
Run Code Online (Sandbox Code Playgroud)
我像这样解除分配3D数组:
for( int i = 0 ; i < 255 ; i++ )
{
for( int j = 0 ; j < 255 ; j++ )
{
cout << i << " " << j << endl;
delete[] DoubleStride_StateTable[i][j] ;
}
delete[] DoubleStride_StateTable[i] ;
}
delete[] DoubleStride_StateTable;
Run Code Online (Sandbox Code Playgroud)
循环只运行两次,即
i=0 j=0
i=0 j=1
Run Code Online (Sandbox Code Playgroud)
然后发生分段错误
我正在做正确的解除分配吗?为什么会出现此错误?
我正在尝试编写一个模板方法来为Direct3D创建着色器.用于创建每种类型着色器的API函数以及着色器的类型具有不同的名称.所以,我写了以下代码:
class Shader final
{
public:
explicit Shader( _In_ ID3DBlob *const pBlob );
template <class T>
void Create
( std::weak_ptr<ID3D11Device>& pDevice
, CComPtr<T>& pResource )
{
auto p_Device = pDevice.lock();
if ( mp_Blob && p_Device )
{
HRESULT hr = E_FAIL;
ID3D11ClassLinkage* pClassLinkage = nullptr; // unsupported for now
pResource.Release();
CComPtr<ID3D11DeviceChild> pRes;
if ( std::is_same<T, ID3D11VertexShader>() )
{
hr = p_Device->CreateVertexShader
( mp_Blob->GetBufferPointer()
, mp_Blob->GetBufferSize()
, pClassLinkage
, reinterpret_cast<ID3D11VertexShader**>( &pRes ) );
}
else if ( std::is_same<T, ID3D11HullShader>() )
{ …
Run Code Online (Sandbox Code Playgroud) #include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
using std::vector;
using std::cout;
using std::ostream_iterator;
int main()
{
vector<int> vec_int{ 1,2,3 };
std::for_each(vec_int.crbegin(), vec_int.rend(), [](const int & i) { cout << i;});
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误是:
C2782:'_ Fn1 std :: for_each(_InIt,_InIt,_Fn1)':模板参数'_InIt'不明确
谁能说出如何正确使用for_each?
我想同时遍历两张地图,我怎么能做到这一点?
我有两个向量都想打印,我可以一次打印两次(auto it : mymap)
吗?就像是:
for (auto it: mymap && auto on: secondMap)
Run Code Online (Sandbox Code Playgroud)
这甚至被允许吗?
我正在尝试打印值,例如(value1, value2)
每个值在不同地图中的位置。映射不一定包含完全相同的项目,但键是一个指令,值是一个整数,所以如果我在映射中有一个元素 for value2
,那么不一定有一个value1
对应于相同键的元素,但在这种情况下它应该是 0,这是默认的整数值。
有任何想法吗?
也许可以组合两个迭代器,每个映射一个?
亲切的问候,Guus Leijsten
I’m in need of some help and guidance on the design of my code. I want to run tests with multiple variables set to multiple values, without creating insane amounts of nested loops. I got a struct which holds various variables like this (only three integers as an example, but the real deal will hold a lot more, including booleans, doubles etc):
struct VarHolder
{
int a;
int b;
int c;
// etc..
// etc..
};
Run Code Online (Sandbox Code Playgroud)
The struct get passed …
最近,我在一个项目中看到他们将defint int键入为BOOL并用它代替了bool。这样做有什么好处吗?
typedef int BOOL;
Run Code Online (Sandbox Code Playgroud) 我编写了一个小程序,以检查异常情况下创建shared_ptr
via new
和make_shared()
函数之间的区别。我到处都读到,通过make_shared()
它是异常安全的。
但是,这两种情况的有趣之处在于,两种情况下的析构函数都不会在堆栈展开后调用?我错过了什么吗?提前致谢。
#include <iostream>
#include <memory>
class Car
{
public:
Car() { cout << "Car constructor!" << endl; throw std::runtime_error("Oops"); }
~Car() { cout << "Car destructor!" << endl; }
};
void doProcessing()
{
// std::shared_ptr<Car> sp(new Car());
std::shared_ptr<Car> sp2 = std::make_shared<Car>();
}
int main()
{
try
{
doProcessing();
}
catch(...)
{
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我在工作中看到了这个,它是我加入之前由人们写的。使用静态变量代替类的静态成员。就目前而言,我看不出为什么不应在此处使用类的静态成员的原因。如果我想说服这里的人们改变它,是否有什么好的借口说服他们?
我试图找到静态成员和静态变量之间的区别,似乎人们倾向于静态成员,除非有充分的理由,否则应该始终使用静态成员,但是没有提到非常现实的情况。
当前代码:
class Foo {
public:
static Foo *get() {
static Foo _instance;
return &_instance;
}
// ...
};
Run Code Online (Sandbox Code Playgroud)
如何使用此功能:
int XXX_loadxxx(const char xxx, foo_handle *handle) {
// just get foo ptr and return
xxx::foo *ptr = xxx::foo::get();
int ret = ptr->init();
if (ret != 0) {
return -1;
}
*handle = ptr;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我心中的代码应如何定义该类:
class Foo {
static Foo _instance;
public:
static Foo *get() {
return &_instance;
}
// ...
};
Run Code Online (Sandbox Code Playgroud)
如果有人能告诉我将其更改为静态成员是否有任何不同,以及原因,我将非常感谢。
我有一个构建器类,我想将参数存储为引用,以便在后续构建中使用。
我想将可变数量的参数传递给我的类,使用类模板参数推导来推断模板参数,并将这些传递的参数作为引用存储在std :: tuple中。
从参数包转换为引用的std :: tuple的最简单方法是什么?
我发现std :: forward_as_tuple的功能与我想要的类似,但是我不想要转发引用,而且它在成员元组的初始化期间提供了语法错误。
template <typename... Ts>
struct Builder
{
using ArgsT = decltype(std::forward_as_tuple(Ts{}...));
ArgsT args_;
Builder(Ts&... ts) : args_(ts...) {}
};
int main()
{
struct Foo
{
int a;
double b;
};
Foo foo{};
Builder fooBuilder{foo.a, foo.b};
}
Run Code Online (Sandbox Code Playgroud)
语法错误是:
错误:没有匹配的调用函数
std::tuple<int&&, double&&>::tuple(int&, double&)