互斥体在许多编程语言中很常见,例如C/C++.我想念他们用Java.但是,我可以通过多种方式编写自己的方法class Mutex:
什么是最快(最好的运行时)方式?我认为同步是最常见的,但性能呢?
标题说:给定std :: tuple,我想
是否有STL提供的解决方案?还是一个解决方法?有人可以尝试完成我的代码吗?
#include <tuple>
int main ()
{
std::tuple<int,char,int> mytuple (10,'a', 5);
// how to get the first int element here? (10)
// int x = std::get_me_the_first<int>(mytuple);
// how to get the type of the second element here?
// std::get_me_type_of<1> ch = 'x';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译如下:
g++ -std=c++11 -Wall main.cpp -o main
Run Code Online (Sandbox Code Playgroud) 以下"最小"示例应显示使用规则3(半).
#include <algorithm>
#include <iostream>
class C
{
std::string* str;
public:
C()
: str(new std::string("default constructed"))
{
std::cout << "std ctor called" << std::endl;
}
C(std::string* _str)
: str(_str)
{
std::cout << "string ctor called, "
<< "address:" << str << std::endl;
}
// copy ctor: does a hard copy of the string
C(const C& other)
: str(new std::string(*(other.str)))
{
std::cout << "copy ctor called" << std::endl;
}
friend void swap(C& c1, C& c2) {
using std::swap;
swap(c1.str, …Run Code Online (Sandbox Code Playgroud) 最小的例子:
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
struct vertex
{
int number;
};
struct edge {};
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::directedS, vertex, edge> graph_t;
typedef boost::graph_traits<graph_t>::vertex_descriptor vertex_t;
typedef boost::graph_traits<graph_t>::edge_descriptor edge_t;
struct vertex_visitor : public boost::default_dfs_visitor
{
void discover_vertex(vertex_t v, graph_t& g)
{
g[v].number = 42;
}
};
int main()
{
graph_t g;
vertex_t v1 = boost::add_vertex(g);
vertex_t v2 = boost::add_vertex(g);
boost::add_edge(v1, v2, g);
vertex_visitor vis;
boost::depth_first_search(g, boost::visitor(vis));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它不起作用,因为图形需要像const在中一样被引用vertex_visitor::discover_vertex().
有没有比编写我自己的DFS算法(或使用const_cast)更好的方法来做我想做的事情?此外,您的解决方案是否允许在发现顶点时添加/删除边和顶点?
QCursor::setPos()在某些条件下不适用于某些平台。例如,在 MacOS 上,有辅助功能设置,允许应用程序允许或禁止控制光标。有没有办法知道QCursor::setPos()目前是否有效果?
最简单的解决方案,使用稍微改变光标QCursor::setPos并检查它是否改变(使用QCursor::pos)不起作用,至少在 MacOS 上不起作用。
注意:需要至少适用于 Linux、Windows、Mac 的通用解决方案。
我尝试计算这个数组的DFT x_1.它必须简单,但我的价值太低了.我的代码出了什么问题?
请不要链接到其他示例 - 只是寻找我自己的代码的修复程序.
#include <iostream>
#include <complex>
#include <cassert>
int main ()
{
const unsigned int N = 20;
const double x_1[N] = {0, 0.3, 0.6, 0.8, 1, 1, 0.9, 0.7, 0.5, 0.2, 0.2, 0.5, 0.7, 0.9, 1, 1, 0.8, 0.6, 0.3, 0};
for(unsigned int k = 0; k < N; k++)
{
std::complex<double> sum(0.0,0.0);
for(unsigned int j = 0; j < N; j++)
{
int integers = -2*j*k;
std::complex<double> my_exponent(0.0, M_PI/N*(double)integers);
sum += x_1[j] …Run Code Online (Sandbox Code Playgroud) 最佳工作示例:
#include <iostream>
struct Printer
{
template<class T>
static void print(T elem) {
std::cout << elem << std::endl;
}
};
template<class printer_t>
struct Main
{
template<class T>
void print(T elem) {
// In this case, the compiler could guess T from the context
// But in my case, assume that I need to specify T.
printer_t::print<T>(elem);
}
};
int main()
{
Main<Printer> m;
m.print(3);
m.print('x');
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的编译器(g ++)给了我错误"在'>'令牌之前预期的primary-expression".怎么了?怎么解决?
C++ 11接受了.
此代码编译时没有任何警告或错误,并且是可执行的.
template<class T>
struct testclass
{
template<int I>
class inner {};
template<int I>
void f(inner<I> ) {}
};
int main()
{
testclass<bool> test;
test.f(testclass<bool>::inner<3>()); // l. 13
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在,我想做的是省略第testclass::13行:
test.f(inner<3>());
Run Code Online (Sandbox Code Playgroud)
这是行不通的.我可以在testclass'定义中添加任何内容,以便我的代码有效吗?
允许使用C++ 11.
鉴于这个最小的例子.
#include <iostream>
#include <string>
void print_ptr(const std::string& s)
{
const char* data = s.data();
std::cout << "ptr: " << (void*)data << std::endl;
}
std::string str_return(const char* suffix)
{
std::string s("prefix");
s += " ";
s += suffix;
print_ptr(s);
return s;
}
int main()
{
std::string s = str_return("suffix"), t;
print_ptr(s);
t = str_return("suffix2");
print_ptr(t);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我这样编译:
g++ -std=c++98 -fno-elide-constructors -g -Wall str_return.cpp -o str_return
Run Code Online (Sandbox Code Playgroud)
我的g ++:
gcc version 4.7.1
Run Code Online (Sandbox Code Playgroud)
输出:
ptr: 0x804b04c
ptr: 0x804b04c
ptr: 0x804b07c
ptr: …Run Code Online (Sandbox Code Playgroud) 最小的例子:
template<template<class ...> class>
struct templ {};
template<class T>
using special = templ<T::type>;
int main() {}
Run Code Online (Sandbox Code Playgroud)
铛++:
test.cpp:5:23: error: template argument for template template parameter must be a class template or type alias template
using special = templ<T::type>;
Run Code Online (Sandbox Code Playgroud)
实际上,我的意思是说这T::type是一个类模板,例如
struct detail1 {
template <class T>
using type = std::vector<T>;
};
struct detail2 {
template <class T>
struct type {};
};
Run Code Online (Sandbox Code Playgroud)
但是怎么能这么说呢?g ++建议使用typename T::type,但这对我来说是错误的,事实上,这并不能解决错误.