CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19
  1. #16
    Join Date
    Apr 2000
    Belgium (Europe)

    Re: std::string to std::vector<TCHAR> with terminator

    Quote Originally Posted by 2kaud View Post
    Why not just replace std::copy with memmove()?
    "specs" :s (I will reserve to keep to myself any impressions on the mental sanity of whomever wrote those specs.)

    It's fine if the compiler optimizes to it, but can't write them myself.

    Quote Originally Posted by superbonzo View Post
    on the contrary, if you have a compliant c++11 compiler a do-nothing default constructor
    Nope, needs to work among others on VS2010 and an older version of GCC. so I can't assume full C++11 compliance.

    anyway, did you considered codeplug's reserve+assign+c_str suggestion ? I think you could even spare the reserve call ( being the const char* returned by c_str() random access iterators, the reserve should be done automatically, but I'm not sure though ... )
    Yes, Even mentioned this before.
    THe problem being that a std::string does not usually have a terminator, and calling c_str() could cause a realloc to insert it, which is the whole thing I'm trying to avoid.

    Quote Originally Posted by Codeplug View Post
    >> I think you could even spare the reserve call
    Yes, but see above.

  2. #17
    Join Date
    Nov 2003

    Re: std::string to std::vector<TCHAR> with terminator

    Does anyone know if a C++03 implementations of std::string where c_str() isn't constant complexity (like it is in C++11)?


    This was an interesting read: https://groups.google.com/forum/#!ms...w/IcWITMiCByYJ

    So it seem the '03 language allowed for a "rope" internal representation, possibly giving c_str() a non-constant complexity - but no implementation is known to have done that.

    In my mind, it's just a "language defect" that was fixed in C++11 - providing a guarantee that was always safely assumed.

    Last edited by Codeplug; October 10th, 2013 at 02:30 PM.

  3. #18
    Join Date
    Feb 2013

    Re: std::string to std::vector<TCHAR> with terminator

    Maybe I'm confused. Unless you need the functionality of the vector... But if you're avoiding reallocation, why not just go with a simple array? :S

    I know this may not help much, but thankfully with C++11, they've provided more constructor overloads.
    int main(void)
    	std::string s("Testing123");
    	std::vector<TCHAR> v(s.size() + 1);
    	std::copy(s.begin(), s.end(), v.begin());
    	v.at(s.size()) = _T('\0');
    	// Testing output
    	for (const auto &obj : v) std::cout << obj << std::endl;
    	return 0;
    Last edited by AceInfinity; October 14th, 2013 at 01:00 AM.

    Microsoft MVP .NET Programming (2012 - Present)
    ®Crestron DMC-T Certified Automation Programmer

  4. #19
    Join Date
    Apr 2000
    Belgium (Europe)

    Re: std::string to std::vector<TCHAR> with terminator

    The vector is a member of a class, and the class interface is "locked". I'm trying to get things to work faster without changing the class interface at all.

    Changing the class requires a "change of spec" form, a lot of hassle getting it approved and it of course requires changing the code that makes use of these classes. (which is a lot of work)

    So the vector constructor "solution" isn't a solution for this scenario.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)