小编Joe*_*oel的帖子

Node.js缓冲区的最大大小是多少

我读到一个文件太大而无法放入缓冲区时出现致命错误.

FATAL ERROR: v8::Object::SetIndexedPropertiesToExternalArrayData() length exceeds max acceptable value
Run Code Online (Sandbox Code Playgroud)

要么,

RangeError:在Function.Buffer.allocUnsafe(buffer.js:209:3)中,"size"参数不得大于2147483647

如果我尝试分配1GB缓冲区,我会得到同样的致命错误,

var oneGigInBytes = 1073741824;
var my1GBuffer = new Buffer(oneGigInBytes); //Crash   
Run Code Online (Sandbox Code Playgroud)

Node.js Buffer类实例的最大大小是多少?

v8 node.js

37
推荐指数
2
解决办法
2万
查看次数

std :: ostream_iterator阻止最后一项使用分隔符

有没有办法使用std :: ostream_iterator(或类似的),以便不为最后一个元素放置分隔符?

#include <iterator>
#include <vector>
#include <algorithm>
#include <string>


using namespace std;
int main(int argc, char *argv[]) {
    std::vector<int> ints = {10,20,30,40,50,60,70,80,90};
    std::copy(ints.begin(),ints.end(),std::ostream_iterator<int>(std::cout, ","));
}
Run Code Online (Sandbox Code Playgroud)

会打印

10,20,30,40,50,60,70,80,90,

我试图避免落后的分隔符.我想要打印

10,20,30,40,50,60,70,80,90

当然,你可以使用一个循环:

for(auto it = ints.begin(); it != ints.end(); it++){
  std::cout << *it;
  if((it + 1) != ints.end()){           
    std::cout << ",";
  }
}
Run Code Online (Sandbox Code Playgroud)

但是考虑到基于C++ 11范围的循环,跟踪位置是很麻烦的.

int count = ints.size();
for(const auto& i : ints){
  std::cout << i;
  if(--count != 0){
    std::cout << ",";
  }     
}
Run Code Online (Sandbox Code Playgroud)

我愿意使用Boost.我查看了boost :: algorithm :: …

c++ iostream stl-algorithm

10
推荐指数
1
解决办法
6620
查看次数

你能不能将C++ RValue引用参数标记为const

我一直在切换模板工厂函数来使用(和理解)std :: forward来支持rvalues和移动语义.我通常用于模板类的样板工厂函数始终将参数标记为const:

#include <iostream>
#include <utility>

template<typename T, typename U>
struct MyPair{
    MyPair(const T& t, const U& u):t(t),u(u){};

    T t;
    U u;
};

template<typename T, typename U>
std::ostream& operator<<(std::ostream& os, const MyPair<T,U>& pair){
    os << "(" << pair.t << ")=>" << pair.u;
    return os;
}

template<typename T, typename U>
MyPair<T,U> MakeMyPair(const T& t, const U& u){
    return MyPair<T,U>(t,u);
}

using namespace std;
int main(int argc, char *argv[]) {    

    auto no_forward = MakeMyPair(num, num);
    std::cout << no_forward << std::endl;

    auto …
Run Code Online (Sandbox Code Playgroud)

c++ const rvalue-reference perfect-forwarding c++11

9
推荐指数
1
解决办法
1141
查看次数

使用Boost Asio接受IPv6链接范围地址

我有一个使用Boost ASIO的TCP服务器.我注意到在Linux上使用链接范围的IPv6地址时我无法创建一个boost::asio::ip::tcp::acceptor没有抛出异常.使用全局IPv6地址或IPv4地址将正常工作.

我很确定问题是范围ID未正确设置但我无法弄清楚如何解决问题.

我正在使用ubuntu提供的boost 1.40.0库在Ubuntu 11.04 LTS上进行开发.这是我所拥有的服务器代码的一个非常愚蠢的版本,它显示了这个问题:

#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <string>

/* To Compile:
g++ -Wall -o ./asio-ipv6 ./asio-ipv6.cpp -lboost_system 
*/

typedef boost::shared_ptr<boost::asio::ip::tcp::socket>   TcpSocketPtr;
typedef boost::shared_ptr<boost::asio::ip::tcp::acceptor> TcpAcceptorPtr;
typedef boost::shared_ptr<boost::asio::ip::tcp::endpoint> TcpEndpointPtr;

class AsioServer{
public:
  AsioServer(boost::asio::io_service& io): io_(io){};

  //throws
  void accept(const std::string& ipString,unsigned short port){
    boost::asio::ip::address addr = boost::asio::ip::address::from_string(ipString);
    std::cout << "Valid IP address " << ipString << std::endl;

    this->endpoint_.reset(new boost::asio::ip::tcp::endpoint(addr,port));
    std::cout << "Created endpoint" << std::endl;

    //Will throw if a link local IPv6 …
Run Code Online (Sandbox Code Playgroud)

c++ boost ipv6 boost-asio

5
推荐指数
1
解决办法
4576
查看次数

如何使用readline功能构建一个扭曲的python客户端

我正在尝试使用Python Twisted为简单的TCP服务器编写客户端.当然我对Python很新,刚刚开始关注Twisted,所以我可能做错了.

服务器很简单,你打算使用nc或telnet.没有身份验证.您只需连接并获得一个简单的控制台.我想编写一个添加了一些readline功能的客户端(历史记录和电子邮件,比如ctrl-a/ctrl-e就是我所追求的)

下面是我编写的代码,就像在命令行中使用netcat一样好 nc localhost 4118

from twisted.internet import reactor, protocol, stdio
from twisted.protocols import basic
from sys import stdout

host='localhost'
port=4118
console_delimiter='\n'

class MyConsoleClient(protocol.Protocol):
    def dataReceived(self, data):
        stdout.write(data)
        stdout.flush()

    def sendData(self,data):
        self.transport.write(data+console_delimiter)

class MyConsoleClientFactory(protocol.ClientFactory):
    def startedConnecting(self,connector):
        print 'Starting connection to console.'

    def buildProtocol(self, addr):
        print 'Connected to console!'
        self.client = MyConsoleClient()
        self.client.name = 'console'
        return self.client

    def clientConnectionFailed(self, connector, reason):
        print 'Connection failed with reason:', reason

class Console(basic.LineReceiver):
    factory = None
    delimiter = console_delimiter

    def __init__(self,factory):
        self.factory = …
Run Code Online (Sandbox Code Playgroud)

python twisted readline

5
推荐指数
1
解决办法
2790
查看次数

在Qi中使用Boost Phoenix引用语法中的上一个匹配项

我是Boost Spirit的新手。理想情况下,我想使用Phoenix来确保语法中的两个值相同。我想要工作的简明版本是两个整数相等的元组。

我想解析一个字符串“ 14,14,test”,但由于“ 14,12,test”失败,因为14不等于12。我想打印以下代码:

Good: (14 14 test)
Fail
Run Code Online (Sandbox Code Playgroud)

目前,由于我允许qi :: int_解析第二个值而没有任何检查,因此两个输入都将通过。

#include <iostream>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/spirit/include/qi_matches.hpp>

#include <string>
#include <vector>
#include <algorithm>

namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;


int main(){


    std::vector<std::string> test_inputs = {"14,14,test","14,12,test"};

    std::for_each(test_inputs.begin(),test_inputs.end(),[](const std::string& input){
        boost::fusion::vector<int,int,std::string> result;
        int i(0);

        auto res = qi::parse(input.begin(),input.end(),
            //works but also parses "14,12,test"
            qi::int_[phx::ref(i) = qi::_1] >> qi::lit(",") >> qi::int_ >> qi::lit(",") >> +qi::char_,
            //Fails to compile
            //qi::int_[phx::ref(i) = qi::_1] >> qi::lit(",") >> qi::int_(phx::val(i)) …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-spirit boost-spirit-qi c++11

3
推荐指数
1
解决办法
636
查看次数