我正在通过构建我自己的vector版本来实践C++,名为"Vector".我有两个构造函数,填充构造函数和范围构造函数.他们的声明如下:
template <typename Type>
class Vector {
public:
// fill constructor
Vector(size_t num, const Type& cont);
// range constructor
template<typename InputIterator> Vector(InputIterator first, InputIterator last);
/*
other members
......
*/
}
Run Code Online (Sandbox Code Playgroud)
填充构造函数用num的num填充容器; 并且范围构造函数将[first,last]范围内的每个值复制到容器中.它们应该与STL向量的两个构造函数相同.
他们的定义如下:
//fill constructor
template <typename Type>
Vector<Type>::Vector(size_t num, const Type& cont){
content = new Type[num];
for (int i = 0; i < num; i ++)
content[i] = cont;
contentSize = contentCapacity = num;
}
// range constructor
template <typename Type>
template<typename InputIterator>
Vector<Type>::Vector(InputIterator first, InputIterator last){
this->content …Run Code Online (Sandbox Code Playgroud) 我使用的是Red Hat Enterprise Linux Server 6.2版,我只有终端访问权限(没有GUI).我的公司有一个代理自动配置脚本(PAC),这是获得互联网连接的唯一方法.
我的Windows机器没问题,因为很容易在IE->工具 - > Internet选项中进行设置.但是在Linux中,特别是没有GUI,似乎没有办法使用这个PAC脚本(我已经谷歌搜索了一段时间).顺便说一句,脚本超过400行的Javascript.
谁知道如何解决这个问题?
我正在玩C++构造函数.这是我的代码:
#include <iostream>
using namespace std;
class ArrayWrapper
{
public:
// default constructor produces a moderately sized array
ArrayWrapper ()
: _p_vals( new int[ 64 ] )
, _size( 64 )
{
cout << "Default constructor: " << this << endl;
}
explicit ArrayWrapper (int n)
: _p_vals( new int[ n ] )
, _size( n )
{
cout << "Constructor: " << this << endl;
}
// move constructor
ArrayWrapper (ArrayWrapper&& other)
: _p_vals( other._p_vals )
, _size( other._size …Run Code Online (Sandbox Code Playgroud) 我在python中编写一个简单的客户端 - 服务器程序.在客户端程序中,我创建了两个线程(使用Python的线程模块),一个用于接收,一个用于发送.接收线程从服务器端连续接收字符串; 而发送线程持续监听用户输入(使用raw_input())并将其发送到服务器端.这两个线程使用队列进行通信(本机同步,LIKE!).
基本逻辑如下:
接收线程:
global queue = Queue.Queue(0)
def run(self):
while 1:
receive a string from the server side
if the string is QUIT signal:
sys.exit()
else:
put it into the global queue
Run Code Online (Sandbox Code Playgroud)
发送帖子:
def run(self):
while 1:
str = raw_input()
send str to the server side
fetch an element from the global queue
deal with the element
Run Code Online (Sandbox Code Playgroud)
如您所见,在接收线程中,我有一个if条件来测试服务器是否已向客户端发送"QUIT信号".如果有,那么我希望整个程序停止.
这里的问题是,在大多数情况下,发送线程被"raw_input()"阻塞并等待用户输入.当它被阻塞时,从另一个线程(接收线程)调用"sys.exit()"将不会立即终止发送线程.发送线程必须等待用户输入内容并按下回车按钮.
任何人都可以激励我如何解决这个问题吗?我不介意使用"raw_input()"的替代品.其实我甚至不介意改变整个结构.
- - - - - - -编辑 - - - - - - -
我在linux机器上运行它,我的Python版本是2.7.5
我在安装GNU Scientific Library(gsl)时遇到了问题.根据所包含的文件,我将软件包放在桌面上,然后执行"./configure","make"和"sudo make install".我检查了/ usr/local/include目录,里面有一个新创建的"gsl"文件夹.但是当我尝试使用库提供的函数时,出现了"对'gsl_sf_beta_inc'的未定义引用"错误.这是我的代码.
#include <stdio.h>
#include <gsl/gsl_sf_gamma.h>
int main (void)
{
double a = 20;
double b = 1000;
double x = 0.5;
double result = gsl_sf_beta_inc(a, b, x);
printf("%f/d", result);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我感觉问题可能是由于我把软件包放在桌面上的事实引起的,所以"make"命令生成的二进制代码就到了那里,这是错误的.那么,我猜是正确的吗?如果是的话,我应该把它们放在哪里?如果不是,我该怎么办?谢谢.
我是C++ 11的新手,我试图在C++ 11中使用线程编写一个简单的程序.我去了合并排序,以下是我的代码:
#include <iostream>
#include <thread>
#include <vector>
using namespace std;
void merge(vector<int>& vec, int start, int mid, int end)
{
vector<int> one (vec.begin() + start, vec.begin() + mid + 1);
vector<int> two (vec.begin() + mid + 1, vec.begin() + end + 1);
int a = 0;
int b = 0;
int index = start;
while (a < one.size() && b < two.size())
{
if (one[a] < two[b])
vec[index ++] = one[a ++];
else
vec[index ++] = two[b ++]; …Run Code Online (Sandbox Code Playgroud) 最近我一直在研究C++ 11中的移动语义.我印象非常深刻,以至于我迫不及待地试着把它弄脏了.以下是我的代码:
#include <iostream>
using namespace std;
class ArrayWrapper
{
public:
// default constructor produces a moderately sized array
ArrayWrapper ()
: _p_vals( new int[ 64 ] )
, _size( 64 )
{
cout << "Default constructor: " << this << endl;
}
explicit ArrayWrapper (int n)
: _p_vals( new int[ n ] )
, _size( n )
{
cout << "Constructor: " << this << endl;
}
// move constructor
ArrayWrapper (ArrayWrapper&& other)
: _p_vals( other._p_vals )
, _size( …Run Code Online (Sandbox Code Playgroud) c++ ×3
c++11 ×3
constructor ×2
linux ×2
c ×1
copy-elision ×1
gsl ×1
installation ×1
mergesort ×1
pac ×1
proxy ×1
python ×1
resolution ×1