我正在工作(主要是为了学习目的)自己的实现,tuple我刚遇到一个问题.我有以下代码:
namespace Rose
{
template<typename T>
struct RemoveReference
{
typedef T Type;
};
template<typename T>
struct RemoveReference<T &>
{
typedef T Type;
};
template<typename... Elems>
class Tuple;
template<typename First, typename... Elems>
class Tuple<First, Elems...>
{
public:
Tuple(First a, Elems... more)
: More(more...), Element(a)
{
}
Tuple<First, Elems...> & operator=(const Tuple<RemoveReference<First>::Type,
RemoveReference<Elems>::Type...> & rhs)
{
this->Element = rhs.Element;
this->More = rhs.More;
return *this;
}
private:
Tuple<Elems...> More;
First Element;
};
template<typename Only>
class Tuple<Only>
{
public:
Tuple(Only a) : …Run Code Online (Sandbox Code Playgroud) 今天,在文章堆损坏的 EFNet C++ Wiki上,我找到了两段代码.
void this_is_bad() /* You wouldn't believe how often this kind of code can be found */
{
char *p = new char[5]; /* spend some cycles in the memory manager */
/* do some stuff with p */
delete[] p; /* spend some more cycles, and create an opportunity for a leak */
}
Run Code Online (Sandbox Code Playgroud)
替代方式:
void this_is_good()
{
/* Avoid allocation of small temporary objects on the heap*/
char p[5]; /* Use the stack instead …Run Code Online (Sandbox Code Playgroud) 我是posix线程库的新手,我尝试从教程中编译示例代码:
g++ -lpthread agreement.cpp -o agreement
Run Code Online (Sandbox Code Playgroud)
但是我无法编译代码并收到以下错误消息:
a3q2.cpp:(.text+0x7e): undefined reference to `sem_open'
a3q2.cpp:(.text+0xab): undefined reference to `sem_wait'
a3q2.cpp:(.text+0x290): undefined reference to `sem_post'
a3q2.cpp:(.text+0x2af): undefined reference to `sem_close'
a3q2.cpp:(.text+0x2bb): undefined reference to `sem_unlink'
collect2: ld returned 1 exit status
make: *** [a3q2_exe] Error 1
Run Code Online (Sandbox Code Playgroud)
我知道编译工作需要-lpthread,但是我可能还需要其他任何选项才能解决问题吗?如果不是我如何安装"正确的"pthread库?
谢谢你的帮助!
我想出了下面的代码来生成100001随机字符串.字符串应该是唯一的.但是,以下代码需要数小时才能完成这项工作.有人能告诉我如何优化它,为什么它这么慢?
string getRandomString(int length) {
static string charset = "abcdefghijklmnopqrstuvwxyz";
string result;
result.resize(length);
for (int i = 0; i < length; i++) {
result[i] = charset[rand() % charset.length()];
}
return result;
}
void main(){
srand(time(NULL));
vector<string> storeUnigrams;
int numUnigram = 100001;
string temp = "";
int minLen = 3;
int maxLen = 26;
int range = maxLen - minLen + 1;
int i =0;
while(i < numUnigram){
int lenOfRanString = rand()%range + minLen;
temp = getRandomString(lenOfRanString);
bool doesithave = false; …Run Code Online (Sandbox Code Playgroud) 23.2中的C++标准草案n3242,在包含容器要求的表中,声明X::reference对于包含容器T必须是lvalue T.然而,因为vector<bool>,vector<bool>::reference是另一个类,用于访问存储在向量中的单个字节位的代理.
这是否意味着标准中定义的std::vector类模板规范T = bool无法满足容器要求?
我正在尝试编写一个函数,它接受两个相同包含类型的容器,例如,两个std::vector<int>s,或a std::list<int>和a std::vector<int>.(但不是a std::vector<int>和a std::vector<double>!)
由于我不太确定应该怎么做,所以我决定先写一个测试程序:
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
struct vector_wrapper
{
template <typename T>
struct instance_wrapper
{
typedef typename std::vector<T> instance;
};
};
struct list_wrapper
{
template <typename T>
struct instance_wrapper
{
typedef typename std::list<T> instance;
};
};
template <typename T, typename C1, typename C2>
void move(typename C1::instance_wrapper<T>::instance& c1, typename C2::instance_wrapper<T>::instance& c2) // line 29
{
while (c1.size() > 0)
{
c2.push_front(c1.back());
c1.pop_back();
}
}
int main()
{ …Run Code Online (Sandbox Code Playgroud) 我曾尝试sysconf(_SC_NPROCESSORS_ONLN)和sysconf(_SC_NPROCESSORS_CONF),但他们都返回的总数(如英特尔CPU的文档中调用它)线(如:超线程线程),而不是物理核心(所谓的核心在提到英特尔网站).
有没有办法获得物理内核的数量,而不是逻辑?计数条目/proc/cpuinfo给出8,类似于调用sysconf,我的处理器是上面链接的.
我对Linux和BSD的解决方案很感兴趣,最好是以C API的形式.
我在标准库字符串的新位置面临内存泄漏.
下面我给出了泄漏显示的代码.
string string1("new string");
char _string[sizeof(string)];
new(_string) string(string1);
Run Code Online (Sandbox Code Playgroud)
使用dbx找到泄漏,如下所示
Actual leaks report (actual leaks: 1 total size: 52 bytes)
Total Num of Leaked Allocation call stack
Size Blocks Block
Address
========== ====== =========== =======================================
52 1 0x43f68 operator new < std::basic_string<char,std::char_traits<char>,std::allocator<char> >::__getRep < std::basic_string<char,std::char_traits<char>,std::allocator<char> >::basic_string < main
Possible leaks report (possible leaks: 0 total size: 0 bytes)
Run Code Online (Sandbox Code Playgroud)
这是真正的内存泄漏还是dbx将其表示为泄漏?
我在c ++应用程序中看到过一次只使用带有头文件和源文件的命名空间声明,如下所示:
#ifndef _UT_
#define _UT_
#include <string>
#include <windows.h>
namespace UT
{
void setRootPath(char* program_path, char* file_path);
char * ConvertStringToCharP(std::string str);
};
#endif
//and then in UT.cpp
#include "UT.h"
namespace UT
{
char * ConvertStringToCharP(std::string str)
{
char * writable = new char[str.size() + 1];
std::copy(str.begin(), str.end(), writable);
writable[str.size()] = '\0';
return writable;
}
void setRootPath(char* program_path, char* file_path)
{
//...
}
}
Run Code Online (Sandbox Code Playgroud)
比使用静态方法定义经典类更好吗?
还是只是简单的课程?
剂量这种方法对编译器链接器有什么好处?
此命名空间中的方法被称为分配.
我试图operator=在模板类中重载.
我有这个模板类:
template <class T>
class Matrice
{
T m,n;
public:
template <class V>
friend Matrice<V>& operator=(const Matrice<V> &);
};
template <class T>
Matrice<T>& Matrice<T>::operator=(const Matrice<T> &M)
{
/*...*/
return *this;
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
template <class T>
class Matrice
{
T m,n;
public:
template <class V>
Matrice<V>& operator=(Matrice<V> &);
};
template <class T>
Matrice<T>& operator=(Matrice<T> &M)
{
/*...*/
return *this;
}
Run Code Online (Sandbox Code Playgroud)
但我仍然得到这个错误:
error C2801: 'operator =' must be a non-static member
Run Code Online (Sandbox Code Playgroud) c++ ×9
linux ×3
templates ×2
bsd ×1
c ×1
c++11 ×1
class ×1
heap ×1
memory ×1
namespaces ×1
overloading ×1
pointers ×1
solaris ×1
system-calls ×1