cstring

Converting a C-style string to a C++ std::string

梦想的初衷 提交于 2019-11-27 13:40:45
问题 What is the best way to convert a C-style string to a C++ std::string ? In the past I've done it using stringstream s. Is there a better way? 回答1: C++ strings have a constructor that lets you construct a std::string directly from a C-style string: const char* myStr = "This is a C string!"; std::string myCppString = myStr; Or, alternatively: std::string myCppString = "This is a C string!"; As @TrevorHickey notes in the comments, be careful to make sure that the pointer you're initializing the

读取中文文件到CString

主宰稳场 提交于 2019-11-27 10:14:19
   CString strFileName = _T("D:\\ai\\100.json"); CFile file; file.Open(strFileName, CFile::modeRead); INT sz = file.GetLength(); WCHAR *wBuf = new WCHAR[sz + 1]; char* buffer_src = new char[sz + 1]; file.Read(buffer_src, sz * sizeof(char)); buffer_src[sz] = '\0'; wBuf[sz] = '\0'; file.Close(); MultiByteToWideChar(CP_UTF8,0,buffer_src,-1,wBuf,sz + 1); CString strJson1(buffer_src); OutputDebugString(strJson1); CString strJson2(wBuf); // wBuf这里已经是中文的了 OutputDebugString(strJson2); //buffer_src转为wBuf一样的非乱码中文 WideCharToMultiByte(CP_ACP,0,wBuf,-1,buffer_src,sz + 1,0,0); CString strJson(buffer_src);

MFC常用类、成员函数、数组类、Cstring类、CTime类、CPoint类

心已入冬 提交于 2019-11-27 08:48:19
MFC数组类 CByteArray: CDWordArray: CPtrArray: CUIntArray: CWordArray: CStringArray: 常用成员函数 1.int Add( ARG_TYPE newElement ); throw( CMemoryException ); 2.TYPE& ElementAt( int nIndex ); 3.void FreeExtra( ); 4.TYPE GetAt( int nIndex ) const 5.int GetSize( ) const; 6.int GetUpperBound( ) const; 7.(1)void InsertAt( int nIndex, ARG_TYPE newElement, int nCount = 1 ); throw( CMemoryException ); (2)void InsertAt( int nStartIndex, CArray* pNewArray ); throw( CMemoryException ); 8.void RemoveAll( ); 9.void SetAt( int nIndex, ARG_TYPE newElement ); 10.void SetAtGrow( int nIndex, ARG_TYPE newElement ); throw

VC++ 字符串操作学习总结

北慕城南 提交于 2019-11-27 03:01:36
vc++中各种字符串(转载) http://www.cnblogs.com/tomin/archive/2008/12/28/1364097.html CString ,BSTR ,LPCTSTR之间关系和区别 CString是一个动态TCHAR数组,BSTR是一种专有格式的字符串(需要用系统提供的函数来操纵,LPCTSTR只是一个常 量的TCHAR指针。 CString 是一个完全独立的类,动态的TCHAR数组,封装了 + 等操作符和字符串操作方法。 typedef OLECHAR FAR* BSTR; typedef const char * LPCTSTR; vc++中各种字符串的表示法 首先char* 是指向ANSI字符数组的指针,其中每个字符占据8位(有效数据是除掉最高位的其他7位),这里保持了与传 统的C,C++的兼容。 LP的含义是长指针(long pointer)。LPSTR是一个指向以‘\0’结尾的ANSI字符数组的指针,与char*可以互换使用,在 win32中较多地使用LPSTR。 而LPCSTR中增加的‘C’的含义是“CONSTANT”(常量),表明这种数据类型的实例不能被使用它的API函数改变, 除此之外,它与LPSTR是等同的。 1.LP表示长指针,在win16下有长指针(LP)和短指针(P)的区别,而在win32下是没有区别的,都是32位

Why do i first have to strcpy() before strcat()?

有些话、适合烂在心里 提交于 2019-11-27 01:26:25
Why does this code produce runtime issues: char stuff[100]; strcat(stuff,"hi "); strcat(stuff,"there"); but this doesn't? char stuff[100]; strcpy(stuff,"hi "); strcat(stuff,"there"); strcat will look for the null-terminator, interpret that as the end of the string, and append the new text there, overwriting the null-terminator in the process, and writing a new null-terminator at the end of the concatenation. char stuff[100]; // 'stuff' is uninitialized Where is the null terminator? stuff is uninitialized, so it might start with NUL, or it might not have NUL anywhere within it. In C++, you can

UTF-8, CString and CFile? (C++, MFC)

故事扮演 提交于 2019-11-27 00:31:20
问题 I'm currently working on a MFC program that specifically has to work with UTF-8. At some point, I have to write UTF-8 data into a file; to do that, I'm using CFiles and CStrings. When I get to write utf-8 (russian characters, to be more precise) data into a file, the output looks like Ðàñïå÷àòàíî: Ñèñòåìà Ïðîèçâîäñòâî and etc. This is assurely not utf-8. To read this data properly, I have to change my system settings; changing non ASCII characters to a russian encoding table does work, but

How to convert CString and ::std::string ::std::wstring to each other?

a 夏天 提交于 2019-11-26 23:42:38
CString is quite handy, while std::string is more compatible with STL container. I am using hash_map . However, hash_map does not support CString as key, so I want to convert CString into std::string . Writing a CString hash function seems to take a lot of time. CString -----> std::string How can I do this? std::string -----> CString: inline CString toCString(std::string const& str) { return CString(str.c_str()); } Am I right? EDIT: Here are more questions: How can I convert wstring , CString to each other? //wstring -> CString, std::wstring src; CString result(src.c_str()); //CString->wstring

What does _T stands for in a CString

独自空忆成欢 提交于 2019-11-26 20:48:14
问题 What does the "T" represents in a string. For example _T("Hello").I have seen this in projects where unicode support is needed.What it actually tells the processor 回答1: _T stands for “text”. It will turn your literal into a Unicode wide character literal if and only if you are compiling your sources with Unicode support. See http://msdn.microsoft.com/en-us/library/c426s321.aspx. 回答2: From MSDN: Use the _T macro to code literal strings generically, so they compile as Unicode strings under

Proper way to copy C-strings

最后都变了- 提交于 2019-11-26 17:23:43
问题 Is there an easy way to copy C-strings? I have const char *stringA , and I want char *stringB to take the value (note that stringB is not const ). I tried stringB=(char*) stringA , but that makes stringB still point to the same memory location, so when stringA later changes, stringB does too. I've also tried strcpy(stringB,stringA) , but it seems that if stringB wasn't initialized to a large enough array, there's a segfault. I'm not super experienced with C-strings though, am I missing

FJUT2019暑假第二次周赛题解

蹲街弑〆低调 提交于 2019-11-26 17:12:09
A 服务器维护 题目大意:   给出时间段[S,E],这段时间需要人维护服务器,给出n个小时间段[ai,bi],代表每个人会维护的时间段,每个人维护这段时间有一个花费,现在问题就是维护服务器[S,E]这段时间,需要最小花费是多少,就是用n个小时间段[ai,bi],覆盖满[S,E]这段时间并且所用的花费最少。 解题思路:   解法一:最短路径 ,这是我一开始想到的用的算法,S到E需要使用最小花费,并且还给出每一小段的路程和花费,感觉就像最短路径裸题吖,但是这题问题在于如何建图,如果你使用常规方法建图是不可行的,因为最短路径是覆盖边,而这题给出的是需要覆盖点,假设[2,4] 我们需要覆盖点2,3,4三个点,而一般最短路径建图[2,4]是覆盖2->3,3->4两条边,我们就少一个覆盖的权,所以我们就把终点+1,[2,4]转换成[2,5],2->3,3->4,4->5,对应了2,3,4三个点,在建图时,假如给定区间[2,3] 覆盖点2,3对应边为2->3,3->4,我们就建一条从2->4的边,对于给定[ai,bi]建一条ai->bi+1的边。还有一个问题就是关于区间重复的覆盖,例如区间[2,5]和区间[3,6],可以覆盖区间[2,6],如果只按照上面建图是无法连通[2,6]的,所以我们需要给每一个点加一个反向边,就是从点i+1 ->点i且权值为0,这样当通过2-