-
October 10th, 2013, 08:42 AM
#16
Re: std::string to std::vector<TCHAR> with terminator
Originally Posted by 2kaud
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.
Originally Posted by superbonzo
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.
Originally Posted by Codeplug
>> I think you could even spare the reserve call
Agreed.
Yes, but see above.
-
October 10th, 2013, 10:20 AM
#17
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)?
[edit]
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.
gg
Last edited by Codeplug; October 10th, 2013 at 02:30 PM.
-
October 14th, 2013, 12:58 AM
#18
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.
Code:
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.
[sigpic][/sigpic]
Microsoft MVP .NET Programming (2012 - Present)
®Crestron DMC-T Certified Automation Programmer
-
October 14th, 2013, 08:39 AM
#19
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|