请考虑以下代码:
#include <iostream>
#include <boost\locale.hpp>
#include <Windows.h>
#include <fstream>
std::string ToUtf8(std::wstring str)
{
std::string ret;
int len = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), str.length(), NULL, 0, NULL, NULL);
if (len > 0)
{
ret.resize(len);
WideCharToMultiByte(CP_UTF8, 0, str.c_str(), str.length(), &ret[0], len, NULL, NULL);
}
return ret;
}
int main()
{
std::wstring wfilename = L"D://Private//Test//???? ??????//??????? ????.txt";
std::string utf8path = ToUtf8(wfilename );
std::ifstream iFileStream(utf8path , std::ifstream::in | std::ifstream::binary);
if(iFileStream.is_open())
{
std::cout << "Opened the File\n";
//Do the work here.
}
else
{
std::cout << …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Wikipedia Link中实现Atkin的Sieve算法,如下所示:
到目前为止我尝试过的是Python中的代码:
import math
is_prime = list()
limit = 100
for i in range(5,limit):
is_prime.append(False)
for x in range(1,int(math.sqrt(limit))+1):
for y in range(1,int(math.sqrt(limit))+1):
n = 4*x**2 + y**2
if n<=limit and (n%12==1 or n%12==5):
# print "1st if"
is_prime[n] = not is_prime[n]
n = 3*x**2+y**2
if n<= limit and n%12==7:
# print "Second if"
is_prime[n] = not is_prime[n]
n = 3*x**2 - y**2
if x>y and n<=limit and n%12==11:
# print "third if"
is_prime[n] = not is_prime[n]
for …Run Code Online (Sandbox Code Playgroud) 我需要在C++中对std :: string进行简单的压缩和解压缩.我查看了这个站点,代码是用于Character数组.我想要实现的是两个功能:
std::string original = "This is to be compressed!!!!";
std::string compressed = string_compress(original);
std::cout << compressed << std::endl;
std::string decompressed = string_decompress(compressed);
std::cout << decompressed << std::endl;
Run Code Online (Sandbox Code Playgroud)
我曾尝试将boost压缩为:
std::string CompressData(const std::string &data)
{
std::stringstream compressed;
std::stringstream decompressed;
decompressed << data;
boost::iostreams::filtering_streambuf<boost::iostreams::input> out;
out.push(boost::iostreams::zlib_compressor());
out.push(decompressed);
boost::iostreams::copy(out, compressed);
return compressed.str();
}
std::string DecompressData(const std::string &data)
{
std::stringstream compressed;
std::stringstream decompressed;
compressed << data;
boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
in.push(boost::iostreams::zlib_decompressor());
in.push(compressed);
boost::iostreams::copy(in, decompressed);
return decompressed.str();
}
Run Code Online (Sandbox Code Playgroud)
但是代码有时会在字符串中给出Null字符,即\ u0000.如果压缩数据包含这些空字符,我该如何处理.返回类型字符串是否正确?我怎样才能实现功能 …
我有一个Config文件,其中包含以下内容:
{
"ip": "127.0.0.1",
"heartbeat": "1",
"ssl": "False",
"log_severity": "debug",
"port":"9999"
}
Run Code Online (Sandbox Code Playgroud)
我用JsonCpp来读取上面配置文件的内容.读取Config File的内容工作正常,但在Config File中写入内容失败.我有以下代码:
#include <json/json.h>
#include <json/writer.h>
#include <iostream>
#include <fstream>
int main()
{
Json::Value root; // will contains the root value after parsing.
Json::Reader reader;
Json::StyledStreamWriter writer;
std::ifstream test("C://SomeFolder//lpa.config");
bool parsingSuccessful = reader.parse( test, root );
if ( !parsingSuccessful )
{
// report to the user the failure and their locations in the document.
std::cout << "Failed to parse configuration: "<< reader.getFormattedErrorMessages();
}
std::cout << root["heartbeat"] << …Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
package com.sarvagya;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Collectors;
public class Streamer {
private static final int LOOP_COUNT = 2000;
public static void main(String[] args){
try{
for(int i = 0; i < LOOP_COUNT; ++i){
poolRunner();
System.out.println("done loop " + i);
try{
Thread.sleep(50L);
}
catch (Exception e){
System.out.println(e);
}
}
}
catch (ExecutionException | InterruptedException e){
System.out.println(e);
}
// Add a delay outside the loop to make sure all daemon threads are cleared before main exits.
try{ …Run Code Online (Sandbox Code Playgroud) 我正在使用 Celery 和 Redis。我的 tasks.py 文件中有以下代码:
from celery import Celery
from faker import Factory
fake = Factory.create()
app = Celery("tasks")
app.conf.broker_url = 'redis://localhost:6379/0'
app.conf.result_backend = 'redis://localhost:6379/0'
@app.task
def twitterDP(hashtag):
if hashtag:
return ["From Twitter " + fake.text(20) + " hashtag # " + hashtag for x in range(5)]
return []
Run Code Online (Sandbox Code Playgroud)
为了运行任务,我还有另一个包含以下代码的脚本:
import zmq
from tasks import twitterDP
from celery.result import AsyncResult
import time
class WorkFlow(object):
def __init__(self):
self.ctx = zmq.Context()
self.socket_pull = self.ctx.socket(zmq.PULL)
self.socket_pull.bind("tcp://127.0.0.1:5860")
def do_work(self):
while True:
recv_msg = …Run Code Online (Sandbox Code Playgroud) 我必须实现Boost线程间通信。考虑以下代码:
#include <boost/thread/thread.hpp>
#include <Windows.h>
void threadA()
{
while(true)
{
std::cout << "From thread A" << std::endl;
Sleep(3000); //pretend to do the work
}
}
void threadB()
{
while(true)
{
std::cout << "From thread B" << std::endl;
Sleep(3000); //pretend to do the work
}
}
int main()
{
boost::thread *th1 = new boost::thread(&threadA);
boost::thread *th2 = new boost::thread(&threadB);
th1->join();
th2->join();
delete th1;
delete th2;
}
Run Code Online (Sandbox Code Playgroud)
如果我运行上面的代码,它将生成两个线程。我想要做的是启动,threadA然后向发送一些消息threadB,该消息在接收时将启动线程。或更笼统地说,如果这两个线程都独立运行,该如何处理通信?