Home » Community » Newbie corner » Casting from std::string to String
Casting from std::string to String [message #50355] |
Wed, 03 October 2018 11:51  |
Giorgio
Messages: 218 Registered: August 2015
|
Experienced Member |
|
|
Hi there,
maybe I did not get enough coffee today, but it seems that my brain can't overcome a very simple task: casting from std::string to String.
Here's the code:
std::vector<std::string> stdWords; //This actually comes from another application I cannot control
Upp::Vector<Upp::String> words;
words.Clear(); //This can be removed?
for(unsigned int i=0;i<stdWords.size();i++)
words.push_back(String(stdWords[i])); // I tried also words.push_back((String)stdWords[i]);
PromptOK(words[0]); //This is to see if casting worked: I should read something, instead I always get a blank prompt
After that I have this: "if(words[0]==name())". Name returns a String and I know that they are equal, but it always returns false. It really don't understand.
Thanks in advance,
gio
|
|
|
Re: Casting from std::string to String [message #50356 is a reply to message #50355] |
Wed, 03 October 2018 12:54   |
Oblivion
Messages: 1211 Registered: August 2007
|
Senior Contributor |
|
|
Hello Giorgio,
Vector::push_back is there for STL compatibility, but you don't really need it here. You can simply use Add method, as there is already a constructor (String(const std::string& s)) for conversion.
E.g:
#include <Core/Core.h>
#include <vector>
using namespace Upp;
CONSOLE_APP_MAIN
{
StdLogSetup(LOG_COUT);
std::vector<std::string> sWords;
Vector<String> uWords;
for(int i = 0; i < 5; i++)
sWords.push_back(std::to_string(i));
for(int i = 0; i < sWords.size(); i++)
uWords.Add(sWords[i]);
for(const auto& s : uWords)
LOG(s);
}
Best regards,
Oblivion
Github page: https://github.com/ismail-yilmaz
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Wed, 03 October 2018 12:59] Report message to a moderator
|
|
|
|
Goto Forum:
Current Time: Thu Jun 12 07:37:38 CEST 2025
Total time taken to generate the page: 0.04207 seconds
|