Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » Casting from std::string to String
Casting from std::string to String [message #50355] Wed, 03 October 2018 11:51 Go to next message
Giorgio is currently offline  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 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1091
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


[Updated on: Wed, 03 October 2018 12:59]

Report message to a moderator

Re: Casting from std::string to String [message #50358 is a reply to message #50356] Wed, 03 October 2018 14:52 Go to previous message
Giorgio is currently offline  Giorgio
Messages: 218
Registered: August 2015
Experienced Member
Thanks!
Previous Topic: RLOG in Linux
Next Topic: MoveableAndDeepCopyOption inherite to use a VectorMap ?
Goto Forum:
  


Current Time: Thu Mar 28 09:42:14 CET 2024

Total time taken to generate the page: 0.00772 seconds