stdstring

Why setting null in the middle of std string doesn't have any effect

旧街凉风 提交于 2019-12-05 21:52:41
Consider #include <string> #include <iostream> int main() { /* hello 5 hel 3 */ char a[] = "hello"; std::cout << a << std::endl; std::cout << strlen(a) << std::endl; a[3] = 0; std::cout << a << std::endl; std::cout << strlen(a) << std::endl; /* hello 5 hel o 5 */ std::string b = "hello"; std::cout << b << std::endl; std::cout << b.length() << std::endl; b[3] = 0; std::cout << b << std::endl; std::cout << b.length() << std::endl; getchar(); } I expect std::string will behave identical to char array a. That's it, insert null character in the middle of the string, will "terminate" the string.

Taking pointer to member std::string::size fails to link with libc++ but works with libstdc++

跟風遠走 提交于 2019-12-05 19:51:40
I'm on a project where I need to use libc++. I'm come up with the following problem: When I try to compile the following code: #include <string> int main() { std::string::size_type (std::string::*function)() const = &std::string::size; return 0; } I get the following error: ld: symbol(s) not found for architecture x86_64 If I use the libstdc++ instead of libc++ I get no errors so the issue should to be related with libc++. Full output below: clang++ --stdlib=libc++ -v main.cpp Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn) Target: x86_64-apple-darwin14.1.0 Thread model: posix "

SHFileOperation copying folders using strings

痞子三分冷 提交于 2019-12-05 18:34:08
I am trying to copy a folder by SHFileOperationA function. Here is my code. int main() { SHFILEOPSTRUCTA sf; int result; string source = "D:\\check\\folder4"; string dest = "D:\\Documents\\test\\folder4"; sf.pFrom = source.c_str( ); sf.pTo = dest.c_str( ); sf.wFunc = FO_COPY; sf.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_SILENT; result = SHFileOperationA(&sf); return 0; } I am not able to understand that how to make the string appended by \0 twice. I tried something like this. string source = "D:\\check\\folder4\\0\\0"; string dest = "D:\\Documents\\test\\folder4\\0\\0"; But, it is

How to get a writable C buffer from std::string?

南楼画角 提交于 2019-12-05 11:10:43
I'm trying to port my code from using MFC's CString to std::string for Microsoft Windows platform. And I'm curious about something. Say in the following example: CString MakeLowerString(LPCTSTR pStr) { CString strLower = pStr ? pStr : L""; CharLower(strLower.GetBuffer()); //Use WinAPI strLower.ReleaseBuffer(); return strLower; } I use strLower. GetBuffer () to obtain a writable buffer to be passed to the CharLower API. But I don't see a similar method in std::string . Am I missing something? And if so, how would you overwrite the method above using std::string ? On my new job we do not use MFC

Error LNK2019: unresolved external symbol “toString(int)”

爱⌒轻易说出口 提交于 2019-12-05 08:40:44
Environment: Windows XP. Visual Studios 2010. Language - C++. I have run into the following link error & have run out of ideas how to fix this problem. I have a project (CnD Device) which links to 2 projects (Messages & Carbon) controlled by my group. I have tried to search for project properties between the 3 projects enter tcp_driver.obj : error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl toString(int)" (?toString@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z) referenced in

Is it possible to use an std::string for read()?

百般思念 提交于 2019-12-05 04:06:37
Is it possible to use an std::string for read() ? Example : std::string data; read(fd, data, 42); Normaly, we have to use char* but is it possible to directly use a std::string ? (I prefer don't create a char* for store the result) Thank's Well, you'll need to create a char* somehow, since that's what the function requires. (BTW: you are talking about the Posix function read , aren't you, and not std::istream::read ?) The problem isn't the char* , it's what the char* points to (which I suspect is what you actually meant). The simplest and usual solution here would be to use a local array: char

STL basic_string length with null characters

萝らか妹 提交于 2019-12-05 03:42:15
Why is it that you can insert a '\0' char in a std::basic_string and the .length() method is unaffected but if you call char_traits<char>::length(str.c_str()) you get the length of the string up until the first '\0' character? e.g. string str("abcdefgh"); cout << str.length(); // 8 str[4] = '\0'; cout << str.length(); // 8 cout << char_traits<char>::length(str.c_str()); // 4 Great question! The reason is that a C-style string is defined as a sequence of bytes that ends with a null byte. When you use .c_str() to get a C-style string out of a C++ std::string , then you're getting back the

How to Fix Visual Studio 2012 error LNK2019: unresolved external symbol \"__declspec(dllimport) public: class std::basic_string?

十年热恋 提交于 2019-12-05 01:15:21
How to fix a Visual Studio 2012 error LNK2019: unresolved external symbol "__declspec(dllimport) public: class std::basic_string? I've been compiling a solution containing one exe and several static lib projects on which the exe depends fine with Visual Studio 2008. The static libs include: TCL: tcl84tsx.lib wxWidgets: wxbase.lib zlib.lib ws2_32.lib xerces-c_2.lib SNMP Research EMANATE: subagent.lib,agent.lib,emanate.lib,pack.lib,mibtable.lib,devkit.lib etc. I loaded the solution into Visual Studio 2012 and have compiled all the projects in the solution but when I try to link the exe, I'm

constexpr with string operations workaround?

Deadly 提交于 2019-12-05 00:51:36
问题 This previously answered question explains why the code I have posted below does not work. I have a follow-up question: is there a workaround that is conceptually equivalent, i.e., achieves compile-time string concatenation, but is implemented in a way that is actually supported by C++11? Using std::string is completely non-essential. constexpr std::string foo() { return std::string("foo"); } constexpr std::string bar() { return std::string("bar"); } constexpr std::string foobar() { return

Test whether libstdc++'s version uses a C++11-compliant std::string

↘锁芯ラ 提交于 2019-12-04 23:45:44
I'm writing some C++11 code that makes assumptions about the nature of std::string that are valid, but represent behavior that was changed in C++11. In the earlier days, libstdc++'s basic_string implementation conformed to the 98/03 requirements, but not to the more strict C++11 requirements. As I understand it, libstdc++ has fixed the issues around basic_string . The problem is that there are many versions of the library that people use which do not implement this fix. And my code may silently fail in many unpleasant ways on them. I would like to have a static_assert fire if the user attempts