我怎样才能将窄幅转换string为宽幅string?
我试过这个方法:
string myName;
getline( cin , myName );
wstring printerName( L(myName) ); // error C3861: 'L': identifier not found
wchar_t* WprinterName = printerName.c_str(); // error C2440: 'initializing' : cannot convert from 'const wchar_t *' to 'wchar_t *'
Run Code Online (Sandbox Code Playgroud)
但我得到上面列出的错误.
为什么我会收到这些错误?我该如何解决它们?
有没有其他方法可以直接将narrow字符串转换为wide字符串?
我对书中的问题13.9提出了一个问题,即"破解编码面试".问题是编写一个支持分配内存的对齐alloc和free函数,在答案中代码如下:
void *aligned_malloc(size_t required_bytes, size_t alignment) {
void *p1;
void **p2;
int offset=alignment-1+sizeof?void*);
if((p1=(void*)malloc(required_bytes+offset))==NULL)
return NULL;
p2=(void**)(((size_t)(p1)+offset)&~(alignment-1)); //line 5
p2[-1]=p1; //line 6
return p2;
}
Run Code Online (Sandbox Code Playgroud)
我对第5行和第6行很困惑.为什么你必须做一个"和"因为你已经为p1添加了偏移?[-1]是什么意思?我在这里先向您的帮助表示感谢.
在Windows中,有一个默认的C库msvcrt.dll。
是否可以编写仅使用 msvcrt.dll 中的函数的简单 C 程序?这将减轻安装最新 VC 运行时的需要。
我认为一种可能的方法是显式指定/NODEFAULTLIB,并使用dll导入过程来导入msvcrt.dll函数。
有人有线索吗?
我正在测试一些字符串池分配器的性能:我认为这里提供的那个调用VirtualAlloc然后分割子分配,以及使用标准C++的类似实现(不直接调用任何Win32 API)和new[].
我希望VirtualAlloc版本更快,因为我认为开销应该比C++少new[]; 但我观察到的结果恰恰相反:使用new[]似乎导致代码比使用较低级别更快VirtualAlloc.
我运行了几次测试(代码是用VS2010 SP1编译的),输出是这样的:
Run Code Online (Sandbox Code Playgroud)String pool using VirtualAlloc: 1280.07 ms String pool using new[]: 799.193 ms
为什么是这样?为什么new[]看起来比快VirtualAlloc?
测试源代码如下:
////////////////////////////////////////////////////////////////////////////
// Testing VirtualAlloc vs. new[].
////////////////////////////////////////////////////////////////////////////
#include <string.h>
#include <wchar.h>
#include <algorithm>
#include <exception>
#include <iostream>
#include <new>
#include <ostream>
#include <stdexcept>
#include <string>
#include <vector>
#include <windows.h>
using namespace std;
//--------------------------------------------------------------------------
// String pool allocator using VirtualAlloc, based on this: …Run Code Online (Sandbox Code Playgroud) C++异常不能跨越COM模块边界.
所以,假设我们在一个COM方法体中,并且调用一些C++ 潜在抛出的方法/函数(这可能因为例如使用STL类而抛出):
STDMETHODIMP CSomeComServer::DoSomething()
{
CppDoSomething(); // <--- This may throw C++ exceptions
return S_OK;
}
Run Code Online (Sandbox Code Playgroud)
Q1.上面的代码是可行的实现吗?例如,如果该代码是上下文菜单shell扩展的一部分,如果C++ CppDoSomething()函数抛出C++异常,那么Explorer会做什么?它是否捕获C++异常并卸载shell扩展?在快速失败的方法之后,它是否只是崩溃资源管理器(使用崩溃转储分析问题成为可能)?
Q2.像这样的实现会更好吗?
STDMETHODIMP CSomeComServer::DoSomething()
{
//
// Wrap the potentially-throwing C++ code call in a safe try/catch block.
// C++ exceptions are caught and transformed to HRESULTs.
//
try
{
CppDoSomething(); // <--- This may throw C++ exceptions
return S_OK;
}
//
// Map C++ std::bad_alloc exception to E_OUTOFMEMORY HRESULT.
//
catch(const std::bad_alloc& ex) …Run Code Online (Sandbox Code Playgroud) 我正在将数据写入CSV文件,然后一旦完成,我将文件复制到另一个目录.
这都是循环,所以当第二次迭代开始时,它会从复制的文件中读取数据.
问题是在第二次迭代开始时文件仍在被复制,这会导致明显的问题.
在第二次迭代开始之前,我如何等待循环中的整个函数完成?它应该能够继续进行任何数量的迭代.
for rule in substring_rules:
substring(rule)
Run Code Online (Sandbox Code Playgroud)
功能:
def substring(rule, remove_rows=[]):
writer = csv.writer(open("%s%s" % (DZINE_DIR, f), "wb"))
from_column = rule.from_column
to_column = rule.to_column
reader = csv.reader(open("%s%s" % (OUTPUT_DIR, f)))
headers = reader.next()
index = 0
from_column_index = None
for head in headers:
if head == from_column:
from_column_index = index
index += 1
if to_column not in headers:
headers.append(to_column)
writer.writerow(headers)
row_index = 0
for row in reader:
if rule.get_rule_type_display() == "substring":
try:
string = rule.string.split(",")
new_value = string[0] …Run Code Online (Sandbox Code Playgroud) 我想从使用UTF-8编码的文件中读取一些文本并将其转换为UTF-16,使用std::wifstream如下所示:
//
// Read UTF-8 text and convert to UTF-16
//
std::wifstream src;
src.imbue(std::locale("???")); // UTF-8 ???
src.open("some_text_file_using_utf8");
std::wstring line; // UTF-16 string
while (std::getline(src, line))
{
... do something processing the UTF-16 string ...
}
Run Code Online (Sandbox Code Playgroud)
是否有UTF-8转换的标准区域设置名称?
是否有可能实现这一目标std::locale?
我正在使用Visual Studio 2013.
注意:
我知道I/O流往往很慢,并且可以使用Win32内存映射文件来加快读取速度,使用MultiByteToWideChar()Win32 API进行转换等等.
但对于这种特殊情况,我想要一个只使用标准C++的解决方案和它的标准库,没有 Boost.
如果C++标准库不能这样做,第二个选项是使用Boost ; 在这种情况下,我应该使用哪个Boost库?
我有一个非常愚蠢的问题(我认为).很长一段时间不用C++编写代码,但我无法弄清楚这个问题.我有这门课:
#include <QList>
class Node {
private:
QList<Node*> _adjacent;
public:
Node();
bool isConnectedTo(Node* n) const;
};
Run Code Online (Sandbox Code Playgroud)
这个实现isConnectedTo():
bool Node::isConnectedTo(Node* n) const{
return _adjacent.contains(n) && n->_adjacent.contains(this);
}
Run Code Online (Sandbox Code Playgroud)
我return在行中收到以下错误:
Node.cpp: In member function ‘const bool Node::isConnectedTo(Node*) const’:
Node.cpp:25:60: error: invalid conversion from ‘const Node* const’ to ‘Node*’ [-fpermissive]
In file included from /usr/include/qt5/QtCore/QList:1:0,
from Node.hpp:5,
from Node.cpp:1:
/usr/include/qt5/QtCore/qlist.h:913:27: error: initializing argument 1 of ‘bool QList<T>::contains(const T&) const [with T = Node*]’ [-fpermissive]
Run Code Online (Sandbox Code Playgroud)
我认为,既然这个方法是常数,那么this就是const Node* const …
我在行上遇到编译错误:
MessageBox(e.getAllExceptionStr().c_str(), _T("Error initializing the sound player"));
Error 4 error C2664: 'CWnd::MessageBoxA' : cannot convert parameter 1 from 'const wchar_t *' to 'LPCTSTR' c:\users\daniel\documents\visual studio 2012\projects\mytest1\mytest1\main1.cpp 141 1 MyTest1
Run Code Online (Sandbox Code Playgroud)
我不知道如何解决这个错误,我尝试了以下方法:
MessageBox((wchar_t *)(e.getAllExceptionStr().c_str()), _T("Error initializing the sound player"));
MessageBox(_T(e.getAllExceptionStr().c_str()), _T("Error initializing the sound player"));
Run Code Online (Sandbox Code Playgroud)
我正在使用"使用多字节字符集"设置,我不想更改它.
C++ dll中定义的函数是:
static double (*Func1)(double);
EXTERN_C __declspec(dllexport) __stdcall double TestDelegate(double (*fun)(double))
{
Func1 = fun;
return Func1(25.0);
}
void My_Real_purpose()
{
SomeClass a;
a.SetFunction(Func1);//Define behaviour of a by C# in runtime
a.DoSomething();//Even I want it runs in another thread!
}
Run Code Online (Sandbox Code Playgroud)
我试着用C#这样调用它:
class A
{
[DllImport("DllName.dll")]
public extern static double TestDelegate(IntPtr f);
public delegate double MyFuncDelegate(double x);
public static double MyFunc(double x)
{
return Math.Sqrt(x);
}
static MyFuncDelegate ff;
static GCHandle gch;
public static double Invoke()
{
ff = new MyFuncDelegate(MyFunc); …Run Code Online (Sandbox Code Playgroud)