boost 字符串分割 split, split_regex ; sscanf()

转载地址:https://blog.csdn.net/gw569453350game/article/details/47400973


以多个字符中的任意一个分割(is_any_of),直接上代码:

#include <boost/algorithm/string.hpp>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
using namespace boost;

int main()
{
    string str1("hello abc-*-ABC-*-aBc goodbye");
    vector<string> SplitVec; 
    // is_any_of 表示其中任何一个 字符 都可以作为分隔符, - 或者 *
    // token_compress_on 表示去掉字符串两端分割出来的空的token,默认是token_compress_off
    split(SplitVec, str1, is_any_of("-*"), token_compress_on)
    return 0;
}

如果分隔符不是字符而是字符串或者多个字符,则:

#include <boost/regex.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
using namespace boost;

int main()
{
  string s = "one->two->thirty-four";
  vector <string> fields;
  split_regex( fields, s, regex( "->" ) );
  return 0;
}
// 编译的时候记得加上libboost_regex:
// target_link_libraries(xxx boost_regex)
        ifstream cfgfile;
	cfgfile.open(cfg_file_name_);
	if (!cfgfile.is_open()) {
		cout << "could not open cfg file:  " << cfg_file_name_.c_str() << endl;
		return 0;
	}

	string temp;
	vector<string> SplitVec;

	
	
		while (getline(cfgfile, temp)) {
			if (temp.compare(string("[before fall down relative X,Y,Z axis degrees:]")) == 0) {
				getline(cfgfile, temp);
				if (sscanf(temp.c_str(), "%f %f %f", &old_degree_.x, &old_degree_.y, &old_degree_.z) != 3)
					return printf("Invalid relative X,Y,Z axis degrees\n"), 0;
				else {
					old_degree_.x = degree2rad(old_degree_.x);
					old_degree_.y = degree2rad(old_degree_.y);
					old_degree_.z = degree2rad(old_degree_.z);				
				}
			}


猜你喜欢

转载自blog.csdn.net/u011722133/article/details/80084540