如果一个类有一个函数调用操作符,它返回对容器的引用(严重编辑):
class client_connection {
public:
concurrent_queue<T>& operator()() { return client; }
concurrent_queue<T> client;
};
Run Code Online (Sandbox Code Playgroud)
另一个有以下成员的班级client_connection:
class remote {
public:
void get_version() {
auto d = ... something to generate data ...
client().push(d.begin(), d.end());
}
...
client_connection client;
};
Run Code Online (Sandbox Code Playgroud)
如果client构造为remote对象的一部分,则此方法有效.但是,如果您通过所有权remote通过unique_ptr如何调用函数调用运算符?
将调用更改client()->push(d.begin(), d.end());为生成的错误消息表明问题是因为推送是在unique_ptr,而不是client_connection.
使用.get()现在正在处理底层对象但没有函数调用操作符:client.get()->push(m.begin(), m.end()); // error: ‘class client_connection<unsigned char>’ has no member named ‘push’
但是,尝试各种组合来调用函数调用操作符都会失败.你如何调用一个包含在一个对象上的函数调用操作符unique_ptr?
我对我的程序执行方式感到困惑.我正在循环以下过程:
1)从输入文件中获取课程的名称
2)输出课程名称的长度
问题是第一个值总是小于字符串的实际值.
我的第一个字符串包含13个字符(包括冒号),但nameOfClass.length()返回12.下一个字符串,字符数为16,实际上nameOfClass.length()返回16.
之后的每个值也返回预期值,它只是第一个返回预期值减去1的值.
这是(减少的)代码:
std::ifstream inf("courseNames.txt");
int numberOfClasses = 10;
string nameOfClass;
for (int i = 0; i < numberOfClasses; i++) {
std::getline(inf, nameOfClass,':');
std::cout << nameOfClass.length() << "\n";
}
Run Code Online (Sandbox Code Playgroud)
该文件看起来像这样(courseNames.txt):
Pre-Calculus:
Public-Speaking:
English I:
Calculus I:
...etc. (6 more classes)
Run Code Online (Sandbox Code Playgroud)
这就是我得到的:
12
16
10
11
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释这个.length()函数的行为吗?
如何使用指针访问std :: string中的各个元素?没有类型转换为const char *是否可能?
#include <iostream>
#include <string>
using namespace std;
int main() {
// I'm trying to do this...
string str = "This is a string";
cout << str[2] << endl;
// ...but by doing this instead
string *p_str = &str;
cout << /* access 3rd element of str with p_str */ << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我试图将存储在函数指针中的函数指针强制boost::any转换为函数指针.
void foo(int, int) {}
template<typename T, typename... Ts>
void bar(T func, Ts... args)
{
boost::any any = func;
boost::any_cast<std::function<void(Ts...)>>(any)(args...);
}
Run Code Online (Sandbox Code Playgroud)
但boost::any_cast抛出一个boost::bad_any_cast,但我不知道为什么,因为类型是相同的.
我打电话bar如下
bar(foo, 0, 1);
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
我正在尝试向 a 添加一个不可见字符(CAN- ASCII 码 24)str,但它只添加了该字符的 ASCII 码(显然是因为我使用的是str(24))。
有没有办法将字符 24 添加到字符串对象中?
这里有很多问题/答案可供理解,Ramp up time但我想为我的测试用例详细介绍.
测试用例:在主页上每5分钟预计有1200个用户.所以它将像5个用户/秒.
我设置了以下内容thread properties:
No. of Threads : 1200
Ramp-up Time - ? [I am not sure what to set]
Loop count - Forever
Scheduler - 300 Seconds[5 Minutes]
Run Code Online (Sandbox Code Playgroud)
有谁可以帮我设置ramp up time我的测试用例?我正在我的本地机器上运行测试.
我想检查服务器可以在5分钟内处理多少用户.我们的期望是1200 users.
我在 Haskell 世界中很新。我正在阅读在线http://learnyouahaskell.com,但我无法理解有关模式匹配部分的一个小细节。我已经写了那些函数
myFunc' (firstLetter:_) = firstLetter -- returns firstLetter of given string
Run Code Online (Sandbox Code Playgroud)
但是,如果我这样做
myFunc' (firstLetter:_) = "Hello" ++firstLetter
Run Code Online (Sandbox Code Playgroud)
调用此函数时出现以下错误
Couldn't match type ‘Char’ with ‘[Char]’
Expected type: [[Char]]
Actual type: [Char]
Run Code Online (Sandbox Code Playgroud)
但是如果我像这样修改函数
myFunc' (firstLetter:_) = "Hello" ++ [firstLetter]
Run Code Online (Sandbox Code Playgroud)
当我调用这个函数时效果很好。我想知道为什么在其他情况下我需要括号。什么实际上是firstLetter。
如果在调用像input_stream >> i;这里i是算术型,抛出异常或设置badbit等,是保证i并没有改变?
在C++ 11中,2D矢量在时间方面如何对抗1D向量?
在给定的2D矢量中,所有内部矢量具有相同的大小.
例如:
std::vector<std::vector<int>> X{10, std::vector<int>(4)};
Run Code Online (Sandbox Code Playgroud)
VS
std::vector<int> Y(40);
Run Code Online (Sandbox Code Playgroud)
当随机访问元素时,矢量的哪个化身会表现得更好?
假设我有一个std::array<SomeType, N>并且我想调用一个函数,它使用迭代器来处理对象,std::array但不知道容器是什么std::array.
SomeType是一个具有公共成员函数doSomething()的类
例如,函数可能是:
template<typename Iterator>
void action(Iterator &beg, Iterator &end) {
for (; beg != end; ++beg)
beg->doSomething();
}
Run Code Online (Sandbox Code Playgroud)
通过以下方式调用此功能:
int main() {
std::array<SomeType, 10> a;
action<std::array<SomeType, 10>::iterator>(a.begin(), a.end());
}
Run Code Online (Sandbox Code Playgroud)
但我想知道这是不是这样做的?特别是因为模板可以用于每个类.有没有办法限制功能,SomeType而不让功能知道容器是一个std::array?
c++ ×7
c++11 ×3
boost ×1
c++14 ×1
containers ×1
haskell ×1
iterator ×1
jmeter ×1
performance ×1
python ×1
python-3.x ×1
stl ×1
string ×1
templates ×1
vector ×1