stdstring

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

烈酒焚心 提交于 2019-12-07 17:20:43
问题 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

SHFileOperation copying folders using strings

*爱你&永不变心* 提交于 2019-12-07 15:55:27
问题 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.

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

前提是你 提交于 2019-12-07 05:42:32
问题 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?

Reverse the string in C++

柔情痞子 提交于 2019-12-06 13:37:56
#include <iostream> #include <cstdlib> using namespace std; main() { beginning: string name; cout << "Hey! Enter your name" << endl; cin >> name; int i = name.length() - 1; do { cout << name[i]; i--; } while (i > -1); cout << " Your reverse name is " << name[i] << endl; cout << name[i] << " Your reverse name is " << endl; goto beginning; } Why the "zsuidakrA" is being displayed before "Your reverse name is" although I have coded like cout<<" Your reverse name is "<<name[i]<<endl; For cout<<name[i]<<" Your reverse name is "<<endl; this line, I have found only "Your reverse name is" but there is

C++ How to calculate the number time a string occurs in a data

牧云@^-^@ 提交于 2019-12-06 06:03:42
问题 I want to measure the following two things: How many times a comma appears in a std::std, e.g. if str ="1,2,3,4,1,2," then str.Count(',') returns me 6 in case of above string The second thing is also similar to the first on but instead of single char i want to calculate the number of occurances of a string e.g str.FindAllOccurancesOF("1,2,") returns me 2 Is there any builtin functions in c++ for calculating this or i need to write custom code for this? 回答1: Using one of the std::string::find

Calling std::~basic_string() in gdb

耗尽温柔 提交于 2019-12-06 05:36:47
As per @EvanED in https://stackoverflow.com/a/11311786/890753 I created a gdb command newstr to create a new std::string and put it in a gdb convenience variable: define newstr set ($arg0)=(std::string*)malloc(sizeof(std::string)) call ($arg0)->basic_string() # 'assign' returns *this; casting return to void avoids printing of the struct. call (void)( ($arg0)->assign($arg1) ) end It works great: (gdb) newstr $foo "hello world" (gdb) p $foo->c_str() $57 = 0xb22e388 "hello world" I use newstr in other custom gdb commands, so for tidyness I also created delstr : define delstr call ($arg0)->~basic

Valgrind memory leak with std::string in std::map

早过忘川 提交于 2019-12-06 03:55:54
问题 Here is the output from Valgrind: ==6519== at 0x4C25885: operator new(unsigned long) (vg_replace_malloc.c:319) ==6519== by 0x4EE65D8: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (new_allocator.h:104) ==6519== by 0x4EE7CE0: char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) (basic_string.tcc:138) ==6519== by 0x4EE80F7: std::basic_string<char, std::char_traits<char>, std:

std::string as a key in std::map using a compare operator

元气小坏坏 提交于 2019-12-06 00:23:42
I'm trying to use a std::string as a key in a std::map however, i'm unable to find() correctly. My code is somewhat complicated and large so this is a small program that demonstrates the problem I'm having. If someone could tell me why this doesn't work, i'd be very grateful. Thanks. #include <stdio.h> #include <string> #include <map> struct comparer { public: bool operator()(const std::string x, const std::string y) { return x.compare(y)==0; } }; int main(int argc, char *argv[]) { std::map<std::string, int, comparer> numbers; numbers.insert(std::pair<std::string,int>("One",1)); numbers.insert

Is std::string::replace() optimized for same length strings?

大城市里の小女人 提交于 2019-12-05 23:27:49
Suppose, most of the time I have below scenario for replacement: std::string line; // "line" contains a big string. std::string from = "abcd"; std::string to = "xy"; // to.length() < from.length() // replace "from" with "to" everywhere in "line" Here the string class has to put "xy" and then erase 2 characters which effectively shifts all character in line towards left. There are so many such replacements happening throughout the life of my code. Now coming to the real question. Below is also acceptable for me: // ... if(to.legnth() < from.length()) to.resize(from.length(), ' '); // now to

Misuse of GL info log null terminating character in std::string?

跟風遠走 提交于 2019-12-05 22:25:34
I have a fairly simple log() method for a GL shader and program convenience classes, since the respective compile and link methods only return a bool , while hiding all the GL calls; e.g., std::string glShader::log () const { std::string info; GLint len = 0; if (!glIsShader(gl_shader_obj)) info = "(invalid shader object)\n"; else glGetShaderiv(gl_shader_obj, GL_INFO_LOG_LENGTH, & len); if (len != 0) { info.resize(static_cast<std::string::size_type>(len)); glGetShaderInfoLog(gl_shader_obj, len, NULL, & info[0]); } return info; } Is this a misuse of the std::string::resize (size_type) argument?