我有一个这样的课:
class MyClass {
MyClass(double *v, int size_of_v){
/*do something with v*/
};
};
Run Code Online (Sandbox Code Playgroud)
我的问题:有什么办法,我可以初始化这样的类而不定义一个double数组并将其提供给构造函数?
我想做的事情如下:
auto x = MyClass({1.,2.,3.}, 3);
Run Code Online (Sandbox Code Playgroud) 我有一个这样的功能:
void f(std::ofstream& ostrm)
{
auto a = Myglobal->getData1();
ostrm << a;
auto b = Myglobal->getData2();
ostrm << b;
auto c = Myglobal->m_data1;
ostrm << c;
auto d = Myglobal->m_data2;
ostrm << d;
//...
auto z = Myglobal->getData1000();
ostrm << z;
}
Run Code Online (Sandbox Code Playgroud)
有什么方法可以创建将成员函数或成员分解为代码的函数作为参数?
(a,b,c,D和z不同类型的)
下面的例子展示了如何计算两个集合的交集。STL 是否提供了不仅可以为 2 组而且可以为N组执行此操作的工具?
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
std::vector<int> v1 = { 1,2,9,3,4,5 };
std::vector<int> v2 = { 9,4,2,7,4,1 };
std::vector<int> v(v1.size() + v2.size());
std::vector<int>::iterator it;
std::sort(v1.begin(), v1.end());
std::sort(v2.begin(), v2.end());
it = std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v.begin());
v.resize(it - v.begin());
std::cout << "Both sets have " << (v.size()) << " elements in common:\n";
for (it = v.begin(); it != v.end(); ++it)
{
std::cout << *it << ' ';
}
std::cout << '\n';
return …Run Code Online (Sandbox Code Playgroud) 我想使用 std::transform 做一些类似于 binary_op 的事情,但有一个附加常量,例如,获得两个向量的乘积: x1 = (10,20,30,40,50) 和 x2 = (2, 4,6,8,10),我们可以写:
#include <iostream>
#include <algorithm>
#include <vector>
double multiply(double x, double y){
return x*y;
}
int main () {
std::vector<int> x1;
std::vector<int> x2;
for (int i=1; i<6; i++)
x1.push_back (i*10); //10,20,30,40,50
for (int i=1; i<6; i++)
x2.push_back (i*2); //2,4,6,8,10
std::transform (x1.begin(), x1.end(), x2.begin(), x1.begin(),multiply);
for (std::vector<int>::iterator it=x1.begin(); it!=x1.end(); ++it)
std::cout << ' ' << *it;
//output: 20,80,180,320,500
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码将 x1 和 x2 按元素相乘并返回 (20,80,180,320,500)。
但是,如果我想计算 x1 …
#include <bits/stdc++.h>
std::unordered_map<std::pair<int,int>, int> mp;
int main()
{
mp[make_pair(1, 2)]++;
}
Run Code Online (Sandbox Code Playgroud)
使用时[] operator,我得到了这个
error: no match for ‘operator[]’ (operand types are ‘std::unordered_map<std::pair<int, int>, int>’ and ‘std::pair<int, int>’)
Run Code Online (Sandbox Code Playgroud)
但是,当用 做同样的事情时std::map,不会发生错误。为什么?
我怎样才能让它工作std::unorderd_m?
我想先制作一个没有大小(vector<int> times)的向量,然后再在类()的构造函数中定义其大小times(size)。
我可以通过使用初始化器列表来做到这一点,如下所示
class A (int size): times(size) {};
Run Code Online (Sandbox Code Playgroud)
但是我的问题是,为什么不能在类似于以下代码的类的构造函数中执行此操作?
我的意思是为什么下面的代码是错误的?
class A
{
public:
A(int size);
private:
std::vector<int> line;
};
A::A(int size)
{
line(size);// here I got the error
}
Run Code Online (Sandbox Code Playgroud)
line(size) 犯错误
我正在尝试member-function使用智能指针实例执行类的。该函数的地址通过值传递,我想通过相应类的智能指针实例调用该值。
我已经试过了:
(registerList.*setRegister)();
Run Code Online (Sandbox Code Playgroud)
但是它出错了:
no match for ‘operator->*
Run Code Online (Sandbox Code Playgroud)
Register 类成员函数:
uint16_t Registers::getSP()
{
return this->sp;
}
Run Code Online (Sandbox Code Playgroud)
代码段:
std::unique_ptr<Registers> registerList;
SetRegisteropcodeLdWordRegister(&Registers::getSP)
void opcodeLdWordRegister(uint16_t (*Registers::setRegister)())
{
(registerList.*setRegister)();
}
Run Code Online (Sandbox Code Playgroud) c++ member-function-pointers smart-pointers unique-ptr c++11
对于这样的一个小例子,我只想接受Tif Tis astruct/class并拒绝内置类型,如“int”、“char”、“bool”等。
template<typename T>
struct MyStruct
{
T t;
};
Run Code Online (Sandbox Code Playgroud) 假设我有一些模板函数,它返回传递给它的一些可迭代对象的中值。
就像是:
template<typename T>
decltype(auto) find_median_sorted(T begin)
{
// some code here
}
Run Code Online (Sandbox Code Playgroud)
现在我想确保我T始终是可迭代的。我正在尝试学习如何concepts在 C++ 中使用,那么我可以concept在这里使用某种方法来确保T它是可迭代的吗?
我确信还有其他方法来检查它是否可迭代,但这是否是一个错误的用例concepts?我还在这里发现了这篇与可迭代元素相关的文章,但我不确定这如何适用于我的情况。
在 Visual Studio 上编译如下:
\ntemplate<typename ArgType, typename ReturnType>\nstruct Test\n{\n using FunctionPointerType = std::conditional_t<\n std::is_same_v<ArgType, void>\n , ReturnType(*)()\n , ReturnType(*)(ArgType)\n >;\n FunctionPointerType Func;\n};\n\nint main()\n{\n Test<void, char> tt;\n}\nRun Code Online (Sandbox Code Playgroud)\n但不能在 Linux g++ 上编译。我得到的错误是
\nerror : invalid parameter type \xe2\x80\x98void\xe2\x80\x99\nRun Code Online (Sandbox Code Playgroud)\n我知道我不能在模板中使用 void,这就是我使用std::conditional_t和 的原因std::is_same_v。
我看不出什么是不正确的,有人可以告诉我吗?
\n