This question already has answers here:
How to construct a std::string from a std::vector<char>?
(7 answers)
Closed 7 years ago.
I have a
std::vector<unsigned char> data;
which contains a binary read file.
If I write it to
std::ofstream outputFile("file", std::ios_base::binary);
I'll see a regular text in it. Then I can read it into std::string which will conatain the text.
Is it possible to copy vector directly to string with the same result?
Use the 6th form of std::string's constructor:
// Example program
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<unsigned char> data{'a', 'b', 'c'};
std::string str(data.begin(), data.end());
std::cout << str << std::endl;
}
Live demo here
From http://www.cplusplus.com/reference/string/string/string/.
Use std::String s(data.begin(), data.end());.
Related
This question already has answers here:
C++: Converting a double/float to string, preserve scientific notation and precision
(2 answers)
Closed 5 years ago.
This question is very clearly in no way an "exact duplicate" of the marked question, and that post does not address my goal.
If I have:
double yuge = 1e16;
And I try to add it to a string like this:
std::string boogers = std::to_string (yuge) + ".csv";
I get a file name of 100000000000000000.csv.
I want a nice compact version like 1e16.csv.
As you can see, I would like to use it as a file name, so output methods arent helpful. Halp! Thanks.
you can use std::stringstream to construct the string instead of + operator.
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
int main()
{
double youge = 1e16;
std::stringstream ss;
ss<<youge<<".csv";
std::string filename = ss.str(); // filename = 1e+16.csv
filename.erase(std::remove(filename.begin(), filename.end(), '+'), filename.end());// removing the '+' sign
std::cout<<filename; // 1e16.csv
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to replace all occurrences of a character in string?
E.g, I have a string, "Hello World" and I want to replace all the "l"s with "1"s. How would I do this? I am new to c++. Most of my background is in Python in which you could just use the .replace method.
Use std::replace.
#include <string>
#include <algorithm>
#include <iostream>
int main() {
std::string str = "Hello World";
std::replace(str.begin(),str.end(),'l','1');
std::cout << str; //He11o Wor1d
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C++ long to string
Easiest way to convert int to string in C++
I am use to Java where I could just use .toString() on almost anything but I am trying a few problems in C++
I can not figure out how to make a long value into a string.
You can either use a string stream:
#include <sstream>
#include <string>
long x;
// ...
std::ostringstream ss;
ss << x;
std::string result = ss.str();
Or you can use Boost's lexical_cast:
#include <boost/lexical_cast.hpp>
std::string s = boost::lexical_cast<std::string>(x);
I think it's a common opinion that this aspect of the language isn't quite as elegant as it could be.
In the new C++11, things are a bit easier, and you can use the std::to_string() function:
#include <string>
std::string s = std::to_string(x);
#include <string>
#include <sstream>
std::ostringstream ss;
long i = 10;
ss << i;
std::string str = ss.str();
You can use a stringstream.
std::ostringstream ss;
ss << aLongNumber;
ss.str()
You use operator << like iostream cout and cin. And you use str() method to get the string.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I tokenize a string in C++?
hello every one i want to divide my string into two parts based on '\t' is there any built in function i tried strtok but it take char * as first in put but my variable is of type string
thanks
#include <sstream>
#include <vector>
#include <string>
int main() {
std::string str("abc\tdef");
char split_char = '\t';
std::istringstream split(str);
std::vector<std::string> token;
for(std::string each; std::getline(split, each, split_char); token.push_back(each));
}
Why can't you use C standard library?
Variant 1.
Use std::string::c_str() function to convert a std::string to a C-string (char *)
Variant 2.
Use std::string::find(char, size_t) to find a necessary symbol ('\t' in your case) than make a new string with std::string::substr. Loop saving a 'current position' till the end of line.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How do I tokenize a string in C++?
pseudocode:
Attributes[] = Split line(' ')
How?
I have been doing this:
char *pch;
pch = strtok(line," ");
while(pch!=NULL)
{
fputs ( pch, stdout );
}
and getting a non-written, stuck, exit file. It's something wrong with this?
Well, the thing isn't even meeting my pseudocode requirement, but I'm confused about how to index tokens (as char arrays) to my array, I guess I should write a 2-dim array?
Use strtok with " " as your delimiter.
This is not quite a dup - for C++ see and upvote the accepted answer here by #Zunino.
Basic code below but to see the full glorious elegance of the answer you are going to have to click on it.
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
int main() {
using namespace std;
string sentence = "Something in the way she moves...";
istringstream iss(sentence);
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
ostream_iterator<string>(cout, "\n"));
}
This hinges on the fact that by default, istream_iterator treats whitespace as its separator. The resulting tokens are written to cout on separate lines (per separator specified in constructor overload for ostream_iterator).
The easiest method is boost::split:
std::vector<std::string> words;
boost::split(words, your_string, boost::is_space());