Advertisement
obernardovieira

Split string to float vector

Aug 11th, 2014
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. void split(std::vector<std::string> &ret, std::string source, std::string delimiter) {
  6.     std::vector<std::string> tmp;
  7.     size_t start = 0, end = 0;
  8.    
  9.     while((end = source.find(delimiter, start)) != std::string::npos) {
  10.         tmp.push_back(source.substr(start, end - start));
  11.         start = end + 1;
  12.     }
  13.     tmp.push_back(source.substr(start, source.length()));
  14.     ret = tmp;
  15.     return;
  16. }
  17.  
  18. void split(std::vector<int> &ret, std::string source, std::string delimiter) {
  19.     std::vector<int> tmp;
  20.     size_t start = 0, end = 0;
  21.    
  22.     while((end = source.find(delimiter, start)) != std::string::npos) {
  23.         tmp.push_back(std::stoi(source.substr(start, end - start)));
  24.         start = end + 1;
  25.     }
  26.     tmp.push_back(std::stoi(source.substr(start, source.length())));
  27.     ret = tmp;
  28.     return;
  29. }
  30.  
  31. void split(std::vector<float> &ret, std::string source, std::string delimiter) {
  32.     std::vector<float> tmp;
  33.     size_t start = 0, end = 0;
  34.    
  35.     while((end = source.find(delimiter, start)) != std::string::npos) {
  36.         tmp.push_back(std::stof(source.substr(start, end - start)));
  37.         start = end + 1;
  38.     }
  39.     tmp.push_back(std::stof(source.substr(start, source.length())));
  40.     ret = tmp;
  41.     return;
  42. }
  43.  
  44. int main() {
  45.    
  46.     std::vector<float> d;
  47.     split(d, "5.4 6.5 7.6", " ");
  48.    
  49.     for(int z=0; z!=d.size(); z++) {
  50.         std::cout << d.at(z)*d.at(z) << std::endl;
  51.     }
  52.    
  53.  
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement