草庐IT

THREAD_SIZE

全部标签

c++ - 字符串类中 size() 和 at() 的不正确行为

我有这个代码:stringtest("żaba");cout输出很奇怪:Word:żabaLength:5Letter:�如您所见,长度应为4,字母:“ż”。如何更正此代码以使其正常工作? 最佳答案 你的问题没有提到编码,所以我要在黑暗中刺探一下,说这就是原因。第一步:阅读TheAbsoluteMinimumEverySoftwareDeveloperAbsolutely,PositivelyMustKnowAboutUnicodeandCharacterSets(NoExcuses!).在那之后,应该清楚“裸字符串”这样的东西并不

C++11 - 无法使用 std::thread 和 std::condition_variable 唤醒线程

当我试图通过另一个线程唤醒一个线程时遇到了一个问题。一个简单的生产者/消费者。代码下方。第85行是我不明白为什么它不起作用的地方。生产者线程填充std::queue并调用std::condition_variable.notify_one()而消费者线程正在等待NOTstd::queue.empty()。在此先感谢您的帮助#include#include#include#include#include#include//requestclassrequest:publicstd::mutex,publicstd::condition_variable,publicstd::queue{

c++ - 在 C++11 智能指针中存储 std::thread

在C++11及更高版本中,像这样直接将std::thread存储为类的成员时,有什么优点或缺点:std::threadmy_thread;与像这样将std::shared_ptr或std::unique_ptr存储到线程相反:std::shared_ptrmy_thread_ptr;是否有任何代码选项比其他选项更好?或者没关系,只需2种不同的方式来处理线程对象。 最佳答案 使用指针(或智能指针)成员可能有一些不太常见的原因,但对于常见用法,似乎是std::thread要么不适用,要么本身就足够灵活:我们可能希望更好地控制对象的生命周

c++ - 如何在你的 C++ 项目中包含 boost::thread?

我需要做什么才能在我的项目中包含boost::thread?我已将整个线程文件夹复制到我的工作路径(我希望能够在多台计算机上运行它)并且我得到了fatalerrorC1083:Cannotopenincludefile:'boost/thread/detail/platform.hpp':Nosuchfileordirectory来自#include"thread/thread.hpp"行什么给了?编辑:即使我只是链接到安装预编译二进制文件的boost文件夹,并且我使用#include我明白了fatalerrorLNK1104:cannotopenfile'libboost_threa

c++ - 使用 std::shared_ptr 对象实例创建 boost::thread

我有以下两个代码段。第一个block按预期编译和工作。但是第二个block不编译。我的问题是,给定下面的代码,当尝试基于由shared_ptr代理的对象实例创建线程时,正确的语法是什么?#include#include#include#includestructfoo{voidboo(){}};intmain(){//Thisworks{foo*fptr=newfoo;boost::threadt(&foo::boo,fptr);t.join();deletefptr;}//Thisdoesn'twork{std::shared_ptrfptr(newfoo);boost::threa

c++ - std::queue<T, list<T>>::size() 在 O(n) 中很慢?

我在使用队列的代码中遇到了意外的性能行为。我意识到当队列中有更多元素时性能会下降。事实证明,使用size()方法是原因。这是一些显示问题的代码:#include#include#include#include"Stopwatch.h"usingnamespacestd;structBigStruct{intx[100];};intmain(){CStopwatchqueueTestSw;typedefBigStructQueueElementType;typedefstd::queue>QueueType;//typedefstd::queueQueueType;//nosurpris

c++ - std::thread Visual Studio 2012 警告

我试图了解如何通过VisualStudio2012使用新的std::thread。我正在尝试编译以下代码。#include#includeclassscoped_thread{std::threadt_;public:explicitscoped_thread(std::thread&t):t_(std::move(t)){if(!t_.joinable())throwstd::logic_error("Nothread");}~scoped_thread(){t_.join();}private:scoped_thread(scoped_threadconst&);scoped_th

c++ - size_type 和 int 之间的区别

#include#includeusingnamespacestd;intmain(){vectorstudent_marks(20);for(vector::size_typei=0;i>student_marks[i];}return0;}我在某处读到,最好使用size_type代替int。它真的会对实现产生巨大影响吗?使用size_type有什么好处? 最佳答案 vector::size_type保证涵盖vector大小的所有可能值范围.一个int不是。请注意vector::size_type通常与std::size_t相同,

c++ - 变长数组 : How to create a buffer with variable size in C++

我目前正在编写一个移动平均线类。目标是在创建Running_Average类的新对象时能够将缓冲区大小指定为构造函数的一部分。#include#include"Complex.h"#include#include#include#includeusingnamespacestd;classRunning_Average{public:doublesum=0;doubleaverage=0;inti;doubleAverage(void);//MemberfunctionsdeclarationvoidAddSample(double);Running_Average(int);};Ru

c++ - 根据 size() 排序 vector

我有一个像vector>v(points);这样的二维vector其中坐标类是:classcoordinate{public:intx;inty;coordinate(){x=0;y=0;}};积分是20。如何基于v[i].size()对单个vectorv[i]进行排序,即基于插入v[i]的坐标对象的数量。??? 最佳答案 1)创建一个根据大小比较两个vector的函数:boolless_vectors(constvector&a,constvector&b){returna.size()2)用它排序sort(v.begin(),v