
Untitled
By: a guest on
May 12th, 2012 | syntax:
None | size: 1.29 KB | hits: 19 | expires: Never
Splitting a character array into strings
std::istringstream iss(the_array);
std::string f1, f2, f3, f4;
iss >> f1 >> f2 >> f3 >> f4;
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <iterator>
int main()
{
// Your files are here, separated by 3 spaces for example.
std::string s("picture1.bmp file2.txt random.wtf dance.png");
// The stringstream will do the dirty work and deal with the spaces.
std::istringstream iss(s);
// Your filenames will be put into this vector.
std::vector<std::string> v;
// Copy every filename to a vector.
std::copy(std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
std::back_inserter(v));
// They are now in the vector, print them or do whatever you want with them!
for(int i = 0; i < v.size(); ++i)
std::cout << v[i] << "n";
}
std::vector<std::string> filenames;
std::string the_array("picture1.bmp file2.txt random.wtf dance.png");
std::istringstream iss(the_array);
while (iss.good())
{
std::string s;
iss >> s;
if (s.length() > 0)
{
filenames.push_back(s);
}
}
for (std::vector<std::string>::const_iterator i = filenames.begin();
i != filenames.end();
i++)
{
std::cout << *i << "n";
}