logo
down
shadow

Pass string or wstring in a function


Pass string or wstring in a function

By : ntudavid
Date : October 22 2020, 06:10 PM
it should still fix some issue char and wchar_t (the underyling types of string/wstring) are not compatible (8 and 16 bits) so you cannot combine them inside a runtime-checking environment.
That is why Windows for example has 2 versions for many functions (MessageBoxA, MessageBoxW etc).
code :


Share : facebook icon twitter icon
How do I tell a wstring that a string I am feeding it is already a wstring?

How do I tell a wstring that a string I am feeding it is already a wstring?


By : user2882586
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Consider using a std::vector instead of a std::string. It's the correct container when you want "a contiguous sequence of bytes."
With a std::vector source container, the code is rather straightforward, assuming you really just want to reinterpret the data (i.e., you really just want to treat the bytes as if they were a sequence of wchar_t):
code :
std::vector<char> v = get_my_wstring_character_data();
if (v.size() % sizeof (wchar_t) != 0)
    throw std::runtime_error("Invalid wstring length");

std::wstring ws(reinterpret_cast<wchar_t*>(&v[0]), 
                reinterpret_cast<wchar_t*>(&v[0] + v.size()));
How to convert hash_map<string, string> to hash_map<wstring, wstring> using the std::transform algorithm?

How to convert hash_map<string, string> to hash_map<wstring, wstring> using the std::transform algorithm?


By : rocslar
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , The keytype in a map is const, you can't assign to it, which is what std::transform does; the loop looks something like this:
code :
for(; first != last; ++first, ++dest)
  *dest = func(*first);  // assignment of key-value pair here
std::for_each(m.begin(), m.end(),
    [&](field_type_map::value_type const& p){
      res.insert(to_wfield_type_map_value(p));
    });
How can I make a function work for both string and wstring?

How can I make a function work for both string and wstring?


By : user3220323
Date : March 29 2020, 07:55 AM
it fixes the issue Have a helper function take a character pointer and delegate to the main function passing a std::basic_string:
code :
template<class charT>
bool isUnique(charT const* p)
{
    return isUnique(std::basic_string<charT>(p));
}
template <typename charT>
bool isUnique(std::basic_string<charT>);
Is it possible to write one function for std::string and std::wstring?

Is it possible to write one function for std::string and std::wstring?


By : Dutchy
Date : March 29 2020, 07:55 AM
Hope this helps Its a good Idea, But I'd build the template on top of std::basic_string rather then general StdStringclass
code :
template<class T>
inline void removeOuterWhitespace(std::basic_string<T>& strInOut)
{
  constexpr auto delim[] = {T(' '),T('\t'),T('\n'),T(0)};
  const auto uiBegin = strInOut.find_first_not_of(delim);

  if (uiBegin == std::basic_string<T>::npos)
  {
    // the whole string is whitespace
    strInOut.clear();
    return;
  }

  const auto  uiEnd   = strInOut.find_last_not_of(delim);
  strInOut = strInOut.substr(uiBegin, uiEnd - uiBegin + 1);
}
How to pass a string literal to a function which takes const std::wstring&

How to pass a string literal to a function which takes const std::wstring&


By : falcon_01
Date : March 29 2020, 07:55 AM
it helps some times I have a function which takes const std::wstring& font_family, i.e. , Try:
Related Posts Related Posts :
  • Compiler error when overriding virtual methods
  • What is the point of function pointers?
  • Which is faster in memory, ints or chars? And file-mapping or chunk reading?
  • ISO file in C++
  • Expected Class-name before { token
  • Can I trust floats or doubles representing integers to retain precision?
  • Qt execute multiple get with QNetworkAccessManager
  • Big and Little endian question
  • Visual Studio 2008 compiles anything in C++ file?
  • C++ pointer to functions, Beginner Question
  • Fastest algorithm for primality test
  • Passing array of pointers to another class
  • Handling macro redefinition without modifying .h files ... C / C++ language
  • Implement SSL with SSPI: How to start?
  • Creating a simple VS2008 visualizer inside autoexp.dat (problem with casting)
  • C++ - Unicode Newline
  • Program crashing with 'std::out_of_range' error
  • Use signed or unsigned char in constructing CString?
  • Parallel execution policies in C++ 17
  • C++ Class Templates (Queue of a class)
  • Convert image into useable byte array in C?
  • pointer to preallocated memory as an input parameter and have the function fill it
  • Animated Image in Win32
  • error on compiling statically linked library created in Qt
  • How to easily pass a very long string to a worker process under Windows?
  • Can a class add friend classes at runtime in C++?
  • C++ priority queue in ascending order by specific method for objects
  • Initialize and declare dynamically multiple variables of random entities in a loop in C++
  • Serializing, integer without libaries , getting weird results
  • Function pointers with default parameters in C++
  • Compilation errors with RapidXML
  • Bad_alloc exception when using new for a struct c++
  • count specific things within a code in c++
  • How to extract 48*48 icon image from resource of an executable file?
  • C++ Convert string (or char*) to wstring (or wchar_t*)
  • C++ include .h includes .cpp with same name as well?
  • Any way in C++ to forward declare a function prototype?
  • boost graph adjacency_list, retrieving a node's parents
  • Copy method optimization in compilers
  • what the problem in this program? it doesn't use the second number, and doesn't print the last line?
  • Need to format character precedence in Strings
  • Cross-Platform Camera API
  • sizeof continues to return 4 instead of actual size
  • String search algorithm used by string::find() c++
  • Two-phase lookup: can I avoid "code bloat"?
  • Strongest cipher available to use with C/C++?
  • Why does my program occasionally segfault when out of memory rather than throwing std::bad_alloc?
  • Firefox XULRunner or TriXul?
  • Does GetSystemInfo (on Windows) always return the number of logical processors?
  • Where is the difference between these code? Shouldn't output be the same?
  • What does it mean mean by "Expected a statement" in C++
  • keybd_event is not working right =(
  • What native C++ profiling tool do you suggest?
  • how to copy char * into a string and vice-versa
  • C++ write to front of file
  • Delphi conversion of C++ header for DVP7010B video card DLL?
  • I keep Getting Control reaches end of non-void function. What am I doing wrong?
  • How to have struct members accessible in different ways
  • Am I abusing Policies?
  • Modern equivalent of BoundsChecker for Visual Studio 2008
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk