CString非常方便,同时std::string与STL容器更兼容.我在用hash_map.但是,hash_map不支持CString作为键,所以我想转换CString成std::string.
编写CString哈希函数似乎需要花费很多时间.
CString -----> std::string
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
std::string -----> CString:
inline CString toCString(std::string const& str)
{
return CString(str.c_str());
}
Run Code Online (Sandbox Code Playgroud)
我对吗?
编辑:
以下是更多问题:
我怎么能转换wstring,CString彼此?
//wstring -> CString,
std::wstring src;
CString result(src.c_str());
//CString->wstring.
CString src;
::std::wstring des(src.GetString());
Run Code Online (Sandbox Code Playgroud)
有什么问题吗?
我怎么能转换std::wstring,std::string彼此?
有什么方法吗?我的电脑是AMD64.
::std::string str;
BOOL loadU(const wchar_t* lpszPathName, int flag = 0);
Run Code Online (Sandbox Code Playgroud)
我用的时候:
loadU(&str);
Run Code Online (Sandbox Code Playgroud)
VS2005编译器说:
Error 7 error C2664:: cannot convert parameter 1 from 'std::string *__w64 ' to 'const wchar_t *'
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
我按照以下方式编写单例c ++:
class A {
private:
static A* m_pA;
A();
virtual ~A();
public:
static A* GetInstance();
static void FreeInstance();
void WORK1();
void WORK2();
void WORK3();
}
}
A* A::GetInstance() {
if (m_pA == NULL)
m_pA = new A();
return m_pA;
}
A::~A() {
FreeInstance() // Can I write this? are there any potential error?
}
void A::FreeInstance() {
delete m_pA;
m_pA = NULL;
}
Run Code Online (Sandbox Code Playgroud)
谢谢!Evan Teran和sep61.myopenid.com的回答是正确的,非常好!我的方式是错的,我希望任何人写这样的代码都可以避免我的愚蠢错误.
我的项目中的单例A有一个智能指针向量,另一个线程也可以编辑这个向量,所以当应用程序关闭时,即使我添加了很多CMutex,它总是变得不稳定.多线程错误+单身错误浪费了我一天.
// ------------------------------------------------ -----------新单例,如果您认为以下示例中存在任何问题,欢迎您进行编辑:
class A {
private:
static A* m_pA;
explicit A();
void …Run Code Online (Sandbox Code Playgroud) 我有一个问题boost::shared_ptr<T>.
有很多线程.
using namespace boost;
class CResource
{
// xxxxxx
}
class CResourceBase
{
public:
void SetResource(shared_ptr<CResource> res)
{
m_Res = res;
}
shared_ptr<CResource> GetResource()
{
return m_Res;
}
private:
shared_ptr<CResource> m_Res;
}
CResourceBase base;
//----------------------------------------------
// Thread_A:
while (true)
{
//...
shared_ptr<CResource> nowResource = base.GetResource();
nowResource.doSomeThing();
//...
}
// Thread_B:
shared_ptr<CResource> nowResource;
base.SetResource(nowResource);
//...
Run Code Online (Sandbox Code Playgroud)
如果Thread_A不关心nowResource是最新的,那么这部分代码会有问题吗?
我的意思是当Thread_B没有SetResource()完全,Thread_A得到一个错误的智能点GetResource()?
线程安全是什么意思?
如果我不关心资源是否是最新的,那么shared_ptr<CResource> nowResource当程序nowResource被释放时会崩溃程序还是会破坏问题 …
我正在使用c ++,我想使用以下代码进行alpha混合.
#define CLAMPTOBYTE(color) \
if ((color) & (~255)) { \
color = (BYTE)((-(color)) >> 31); \
} else { \
color = (BYTE)(color); \
}
#define GET_BYTE(accessPixel, x, y, scanline, bpp) \
((BYTE*)((accessPixel) + (y) * (scanline) + (x) * (bpp)))
for (int y = top ; y < bottom; ++y)
{
BYTE* resultByte = GET_BYTE(resultBits, left, y, stride, bytepp);
BYTE* srcByte = GET_BYTE(srcBits, left, y, stride, bytepp);
BYTE* srcByteTop = GET_BYTE(srcBitsTop, left, y, stride, bytepp);
BYTE* maskCurrent = …Run Code Online (Sandbox Code Playgroud) 我正在使用GDI + Graphic将4000*3000图像绘制到屏幕上,但它确实很慢.大约需要300毫秒.我希望它只占用不到10毫秒.
Bitmap *bitmap = Bitmap::FromFile("XXXX",...);
Run Code Online (Sandbox Code Playgroud)
// - - - - - - - - - - - - - - - - - - - - - - // 这部分需要大约300毫秒,太可怕了!
int width = bitmap->GetWidth();
int height = bitmap->GetHeight();
DrawImage(bitmap,0,0,width,height);
Run Code Online (Sandbox Code Playgroud)
// ------------------------------------------
我不能使用CachedBitmap,因为我想稍后编辑位图.
我怎样才能改进它?或者有什么不对吗?
这个原生GDI函数也将图像绘制到屏幕上,只需1毫秒:
SetStretchBltMode(hDC, COLORONCOLOR);
StretchDIBits(hDC, rcDest.left, rcDest.top,
rcDest.right-rcDest.left, rcDest.bottom-rcDest.top,
0, 0, width, height,
BYTE* dib, dibinfo, DIB_RGB_COLORS, SRCCOPY);
Run Code Online (Sandbox Code Playgroud)
// ------------------------------------------------ --------------
如果我想使用StretchDIBits,我需要传递BITMAPINFO,但是如何从Gdi + Bitmap对象获取BITMAPINFO?我通过FreeImage lib做了实验,我使用FreeImageplus对象调用StretchDIBits,它绘制得非常快.但是现在我需要绘制Bitmap,并在Bitmap的位数组上编写一些算法,如果我有Bitmap对象,如何获得BITMAPINFO?这真的很烦人-___________- |
为什么我不能在NTFS文件系统中创建路径中的字符大于255的深层路径?它似乎是FAT32的限制,但也存在于NTFS中?有人可以提供一些文件吗?
非常感谢!
在开发照片查看器应用程序时遇到了问题.我使用ListBox来显示图像,它包含在ObservableCollection中.我将ListBox的ItemsSource绑定到ObservableCollection.
<DataTemplate DataType="{x:Type modeldata:ImageInfo}">
<Image
Margin="6"
Source="{Binding Thumbnail}"
Width="{Binding ZoomBarWidth.Width, Source={StaticResource zoombarmanager}}"
Height="{Binding ZoomBarWidth.Width, Source={StaticResource zoombarmanager}}"/>
</DataTemplate>
<Grid DataContext="{StaticResource imageinfolder}">
<ScrollViewer
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Disabled">
<ListBox Name="PhotosListBox"
IsSynchronizedWithCurrentItem="True"
Style="{StaticResource PhotoListBoxStyle}"
Margin="5"
SelectionMode="Extended"
ItemsSource="{Binding}"
/>
</ScrollViewer>
Run Code Online (Sandbox Code Playgroud)
我还使用滑块绑定ListBox中的Image'height.(滑块的Value也绑定到zoombarmanager.ZoomBarWidth.Width).但我发现如果集合变得更大,例如:包含超过1000个图像,如果我使用滑块来改变iamges的大小,它会变得有点慢.我的问题是.1.为什么变慢?变得它试图缩放每个图像,或者只是因为通知("宽度")被调用超过1000次.2.有没有什么方法可以解决这类问题并加快速度.
PhotoListBoxStyle是这样的:
<Style~~ TargetType="{x:Type ListBox}" x:Key="PhotoListBoxStyle">
<Setter Property="Foreground" Value="White" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}" >
<WrapPanel
Margin="5"
IsItemsHost="True"
Orientation="Horizontal"
VerticalAlignment="Top"
HorizontalAlignment="Stretch" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style~~>
Run Code Online (Sandbox Code Playgroud)
但是如果我使用上面的Style,我必须在ListBox之外使用ScrollViewer,否则我不知道如何获得平滑的滚动滚动条并且wrappanel似乎没有默认的滚动条.有人帮吗?据说滚动查看器的列表框性能不佳.
我在C++的应用程序中使用boost :: shared_ptr.内存问题非常严重,应用程序占用大量内存.
但是,因为我将每个新建对象放入shared_ptr,当应用程序退出时,不会检测到内存泄漏.
必须有一些像std::vector<shared_ptr<> >池持有资源的东西.调试时如何知道谁拥有shared_ptr?
很难逐行检查代码.代码太多......
非常感谢!
似乎ObservableCollection仅支持从UI线程添加,删除,清除操作,如果它由NO UI线程操作,则抛出Not Support Exception.我试图覆盖ObservableCollection的方法,不幸的是,我遇到了很多问题.任何人都可以提供一个可由多线程操作的ObservableCollection示例?非常感谢!