我对如何实例化此模板感到有些困惑。我知道,简单地使用friend会员身份会实现我想要的东西会更容易,但是如果我强迫这样做,该怎么办?我只是想弄清楚。(顺便说一句,我知道这个模板似乎毫无意义),我只想使其编译即可。
#include <iostream>
template <typename T>
inline std::ostream& operator<< (std::ostream& os, const T& date)
{
os << date.getD() << " " << date.getM() << " " << date.getY() << "\n";
return os;
}
class Date
{
private:
int dd, mm, yy;
public:
Date(int d, int m, int y) : dd(d), mm(m), yy(y) {}
int getD() const;
int getM() const;
int getY() const;
};
int Date::getD() const { return dd; }
int Date::getM() const { return mm; }
int …Run Code Online (Sandbox Code Playgroud) 我写了函数。但老师告诉我,在3 个的参数的std::count_if功能,有必要通过拉姆达找出如果这封信是元音。
我不知道如何在那里转移它。
unsigned CalculateVowels(const std::string& str)
{
const std::string& vowels = "aeiouAEIOU";
unsigned count = std::count_if(str.begin(), str.end(), [](int index) {return str[index] == vowels[index]; })
return count;
}
Run Code Online (Sandbox Code Playgroud) 我有一个在本地函数中可以正常工作的代码:
struct Divider
{
public:
size_t factor;
size_t next;
};
void foo()
{
auto cmp = [](const Divider& x, const Divider& y) { return x.next > y.next; };
std::priority_queue < Divider, std::vector<Divider>, decltype(cmp)> sieve(cmp);
// ...
}
Run Code Online (Sandbox Code Playgroud)
我现在想将我的sieve变量移到一个类中。我可以写出以下怪物:
class Bar
{
inline static auto cmp = [](const Divider& x, const Divider& y) { return x.next > y.next; };
std::priority_queue < Divider, std::vector<Divider>, decltype(cmp)> sieve = std::priority_queue < Divider, std::vector<Divider>, decltype(cmp)>(cmp);
};
Run Code Online (Sandbox Code Playgroud)
有什么方法可以编写这种默认构造而无需两次指定类型?或者只是以一种更清洁的方式。
我目前正在学习数据结构和算法课程,我的教授给了我们材料,其中包括接收指针值和指针/引用值的函数。
这些函数看起来像这样:
int function1(int a); // Pass by value
int function2(int &ref); // Pass by reference
int function3(int* ptr); // This will take in a pointer value
int function4(int*& ptr); // This will take in a pointer/reference value
Run Code Online (Sandbox Code Playgroud)
我了解按值传递和按引用传递之间的区别。我也尝试将后两个示例都实现为基本函数,但我不完全确定这两个参数与引用传递有何不同,或者它们之间有何不同。
有人可以解释这两个函数参数是如何工作的以及它们如何实际使用吗?
在双向迭代器有没有像奢侈品随机访问迭代器,因此需要依赖于std::next和std::prev,当有人需要做的操作,如
std::set<int> s{ 1, 2, 3, 4, 5 };
//std::set<int> s2(s.cbegin(), s.cbegin() + 2); // won't work as there is no operator+ for std::set::const_iterator
std::set<int> s2(s.cbegin(), std::next(s.cbegin(), 2));
//std::set<int> s3(s.cbegin(), s.cend() - 2); // won't work as there is no operator- for std::set::const_iterator
std::set<int> s3(s.cbegin(), std::prev(s.cend(), 2));
Run Code Online (Sandbox Code Playgroud)
但是,我们可以实现这些 operator+并operator-使用上面的std::next和std::prev.
#include <set>
#include <iterator> // std::iterator_traits, std::next, std::prev
template<typename InputIt>
constexpr InputIt operator+(InputIt it,
typename …Run Code Online (Sandbox Code Playgroud) c++ iterator operator-overloading c++-standard-library language-lawyer
有时用函数表示值更容易,而 lambda 很适合这样做。但是有没有办法从 lambda 声明中返回值呢?
例如:
int i = []{return 2;};
Run Code Online (Sandbox Code Playgroud)
产生错误。如何使 lambda 声明返回2而不添加任何新代码行?
例如,我试图找出如何反转,grades{1, 2, 3, 4, 5, 6}从第三个元素开始。
我知道我们无法通过列表来(grades.begin() + 2)获得该职位,但我不知道如何去做。这是我到目前为止所拥有的,我只是颠倒了整个列表:
reverse(firstList.begin(), firstList.end());
Run Code Online (Sandbox Code Playgroud)
我希望它是相反的,以便列表变成:grades{1, 2, 6, 5, 4, 3}
昨天,我试图编写一个基本渲染器,渲染器在数据加载到着色器时控制渲染器,而渲染对象不知道所使用的着色器.作为一个顽固的人(而不是睡眠不足),我花了几个小时试图将函数指针发送到渲染器,保存,然后在适当的时间运行.直到后来我意识到我想要构建的是一个消息系统.它让我想知道,是否可以直接保存带参数的函数指针,以便稍后在c ++中运行.
我最初的想法看起来像这样:
//set up libraries and variables
Renderer renderer();
renderable obj();
mat4 viewMatrix();
// renderer returns and object id
int objID = renderer.loadObj(obj)
int main()
{
//do stuff
while(running)
{
//do stuff
renderer.pushInstruction(//some instruction);
renderer.render();
}
}
// functionPtr.h
#include <functional>
class storableFunction
{
public:
virtual ~storableFunction = 0;
virtual void call() = 0;
};
template<class type>
class functionPtr : public storableFunction
{
std::function<type> func;
public:
functionPtr(std::function<type> func)
: func(func) {}
void call() { func(); }
}; …Run Code Online (Sandbox Code Playgroud) 我有这堂课
class A {
unordered_map<string, unordered_set<string>> n_;
public:
A(unordered_map<string, unordered_set<string>>& n) : n_{n} {}
};
Run Code Online (Sandbox Code Playgroud)
我希望能够将构造函数与该语法一起使用
int main() {
A a{{"C", {"A", "B"}}};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是以现在的方式写的,我出错了
error: no matching function for call to `‘A::A(<brace-enclosed initializer list>)’ A a{{"C", {"A", "B"}}};`
Run Code Online (Sandbox Code Playgroud)
如何解决?