使用线程打印奇偶数 我遇到了这个问题,想讨论 C++ 中的解决方案。我可以想到使用 2 个二进制信号量奇数和偶数信号量。偶数信号量初始化为 1,奇数信号量初始化为 0。
**T1 thread function**
funOdd()
{
wait(even)
print odd;
signal(odd)
}
**T2 thread function**
funEven()
{
wait(odd)
print even
signal(even)
}
Run Code Online (Sandbox Code Playgroud)
除此之外,如果我的函数只生成数字并且有第三个线程 T3 将打印这些数字,那么理想的设计应该是什么?我使用了一个数组,其中奇数将放置在奇数位置,偶数将放置在偶数位置。T3 将从这个数组中读取,这将避免对该数组的任何线程安全,如果 T3 没有找到任何索引,那么它将等待该索引被填充。另一种解决方案是使用一个队列,该队列将有一个互斥锁,T1 和 T2 在插入时可以使用该互斥锁。
请对此解决方案发表评论,以及如何使其更有效率。
编辑以使问题更清楚:总体问题是我有两个生产者(T1,T2)和一个消费者(T3),并且我的生产者是相互依赖的。
我正在寻找一个 C++ 位集实现,它可以回答是否在一个范围内设置了一个位。 std::bitset、vector和boost::dynamic_bitset都可以访问我可以循环的各个位,但这不是查询一系列位以询问是否设置了任何位的最有效方法 - 我不这样做甚至不需要知道是哪一个。
bitset b;
if(b.any(33, 199))
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
有提供这个的图书馆吗?我想针对其他实现(包括我可能必须编写的实现)运行一些基准测试,但我找不到任何似乎实现此功能的基准。
我很难理解Tarjan的发音点算法。我目前在这里关注此教程:https : //www.hackerearth.com/practice/algorithms/graphs/articulation-points-and-bridges/tutorial/。我真正看不到的东西,以及在其他任何教程中都看不到的东西,正是“后边缘”的含义。考虑到此处给出的图形,我知道3-1和4-2是后边缘,但是2-1、3-2和4-3也是后边缘吗?谢谢。
algorithm graph-theory graph depth-first-search tarjans-algorithm
在/sf/answers/659694801/之后,我试图比较三个数字:
#include <iostream>
int main() {
std::cout << std::min({2,5,1}) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是编译器给了我错误:
error: no matching function for call to ‘min(<brace-enclosed initializer list>)’
Run Code Online (Sandbox Code Playgroud)
但是,代码在使用时编译得很好
std::min(std::min(2,5),1)
Run Code Online (Sandbox Code Playgroud)
但是第一种方法应该适用于 c++11 标准。我可能做错了什么?
这是我的代码。 已编辑
#include<iostream>
#include<vector>
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
vector<string> v;
for(int i=0;i<n;i++)
{
string temp;
cin>>temp;
v.push_back(temp);
//if(v.size()==1){
// continue;
//}
//cout<<i<<endl;
// cout<<endl;
//cout<<v.size();
int k=i;
while(v[k-1].length()>v[k].length()&&k>-1)
{
swap(v[k],v[k-1]);
k--;
}
// cout<<endl;
}
bool check=true;
for(int i=0;i<v.size()-1;i++)
{
//cout<<v[i]<<endl;
//cout<<v[i+1]<<endl;
if (v[i+1].find(v[i]) != std::string::npos) {
//std::cout << "found!" << '\n';
continue;
}
// cout<<"false"<<endl;
check=false;
}
if(check==true)
{
cout<<"YES"<<endl;
for(int i=0;i<n;i++)
{
cout<<v[i]<<endl;
}
}else{
cout<<"NO"<<endl;
}
}
Run Code Online (Sandbox Code Playgroud)
这个错误的原因是什么: 输入是: …
在下面的代码中,很明显,myFn带有两个参数的函数定义应该来自namespace N。但是编译器无法编译它。它是编译器(g ++ 8.3)的限制,还是由C ++标准强加的?
#include <bits/stdc++.h>
using namespace std;
namespace N
{
// Same name function exists in class A
void myFn(int a, int b)
{
cout << a << ' ' << b << endl;
}
}
using namespace N;
class A {
public:
void myFn(int a)
{
#ifdef FINE
// Explicitly specify where should myFn definition come from
N::myFn(a, a);
#else
myFn(a, a);
#endif
}
};
int main()
{
A a;
a.myFn(3);
return 2; …Run Code Online (Sandbox Code Playgroud) 似乎std::string是位于的仅标头文件Community/VC/Tools/MSVC/?/include/xstring,所有生成的代码都应包含在构建目标中。
如果我是对的,Microsoft如何保证下一个Visual Studio版本不会更改xstring以及其std::string内部结构?
更新1:
我对此问题有很多反对意见,所以让我解释一下为什么我决定问这个问题。
我面临着奇怪的崩溃,而且我不明白为什么会这样。我使用最新的Qt 5.13.0(MSVC2017_x64),并且我使用Visual Studio 2017编译了一些外部库。所有库都已/MDd通过dumpbinutil 进行了检查。
当我尝试运行任何调用Qt库和的代码时std::string,我得到了错误的结果(并最终崩溃)。
这是一个非常简单的示例:
#include <QApplication.h>
int main(int argc, char** argv) {
QString s1("Test");
std::string s2 = s1.toStdString(); // here we have s2 variable with wrong internal structure
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的想法是QtCore DLL库具有std::string与std::stringVisual Studio 2017 不兼容的内部结构。但是Qt是使用Visual Studio 2017创建的(可能与我当前的Visual Studio不同,因为有几个次要版本),所以我决定问一下它们是否兼容。
更新2:
问题出在_ITERATOR_DEBUG_LEVEL。看起来Qt是用2级编译的,而我所有的外部库和应用程序都是用0级编译的。
此选项影响许多C ++标准库类的内部结构,并带来此类副作用。因此,当我们进入内部toStdString()并创建时std::string,我们具有2级和一个内部结构。当我们在应用程序代码中时,我们具有0级和另一个内部结构。我们将具有一种内部结构的对象分配给具有另一种内部结构的对象。
无论如何,我现在对某些内部结构有了更好的了解。
我尝试为堆和堆栈内存中的10 ^ 7整数分配空间,以查看哪一个更快。显然,在堆内存中分配要快得多,但是我不明白原因。
#include <bits/stdc++.h>
#include <chrono>
using namespace std;
using namespace std::chrono;
int main()
{
high_resolution_clock::time_point t1 = high_resolution_clock::now();
int *p = new int[1e7];
high_resolution_clock::time_point t2 = high_resolution_clock::now();
auto duration = duration_cast<microseconds>( t2 - t1 ).count();
cout << duration / 1e6 << "\n"; // 5e-06
t1 = high_resolution_clock::now();
vector<int> v(1e7);
t2 = high_resolution_clock::now();
duration = duration_cast<microseconds>( t2 - t1 ).count();
cout << duration / 1e6 << "\n"; // 0.112284
return 0;
}
Run Code Online (Sandbox Code Playgroud) 这是我的代码
#include <bits/stdc++.h>
class A{
int val;
char c;
};
class B:public A{
char val;
};
struct C{
int val;
char c;
};
struct D:public C{
char val;
};
int main()
{
std::cout<<sizeof(B)<<std::endl; //8
std::cout<<sizeof(D)<<std::endl; //12
}
Run Code Online (Sandbox Code Playgroud)
为什么class有不同的对齐方式struct
*** Dumping AST Record Layout
0 | class A
0 | int val
4 | char c
| [sizeof=8, dsize=5, align=4
| nvsize=5, nvalign=4]
*** Dumping AST Record Layout
0 | class B
0 | class A (base)
0 …Run Code Online (Sandbox Code Playgroud) 我试图找到两个字符串的最大值,它在第一种情况下(传递std::string变量时)给出了正确的答案,但在第二种情况下(传递直接字符串时)给出了错误。
#include<bits/stdc++.h>
using namespace std;
int main()
{
// Case 1
string str1 = "abc", str2 = "abcd";
cout << max(str1, str2) << endl;
// Case 2
cout << max("abc", "abcd") << endl;
}
Run Code Online (Sandbox Code Playgroud) c++ ×9
algorithm ×2
std ×2
bitset ×1
c++11 ×1
graph ×1
graph-theory ×1
heap-memory ×1
inheritance ×1
layout ×1
max ×1
min ×1
namespaces ×1
padding ×1
performance ×1
qt ×1
semaphore ×1
stack-memory ×1
stdstring ×1
struct ×1
visual-c++ ×1