I have heard there is a C++ split function, but I have not yet found anything to help me work with what I have. I have the following:

001E3897  1024   HV        regread set                                                 [rootPos: 0001CBE7, offset 1862832 ]

I need to be able to take this and split it into 5 pieces. I tried doing some weird getline stuff using '\t' but it didn't work out.

This is the code that is being used in C# that I am trying to convert over using C++ functions.

var splitAsm = line.Split(new[] { '|' }, 2);
                    var words = splitAsm[0].Split(new[] { ' ', '\t' }, 4, StringSplitOptions.RemoveEmptyEntries);

Any ideas? I am sure this is easy, but I must be missing something small....Ty

Recommended Answers

All 6 Replies

If you are using C++/CLI like you were, a String ^ has an identical ->Split method to the one in C# (since it's all .NET). I think it requires a cli::array<String ^> to accept the pieces.

I suppose you could use a stringstream, but you'd have to convert your String ^ to a std::string by marshaling.

Ty, I will check it out, I am converting this program so it uses almost no C++/CLI and all standard C++.

I came up with the following to test it out, and I can't seem to split on tabs, what am i doing wrong here?

// Buffer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#using <mscorlib.dll>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>


std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems);
std::vector<std::string> split(const std::string &s, char delim);

int _tmain(int argc, _TCHAR* argv[])
{
	using namespace std;
	ifstream inFile;
	inFile.open("AoE2Wide_1.0e.patch", ios_base::in);
	char str1[1024];
	for (int i = 0; i < 6; i ++)
		inFile.getline(str1, 1024);
	inFile.getline(str1, 1024);
	vector<string> temp = split(str1, '\'\\t\''); // i tried using split(str1, '\t' too, still didnt work
	cout << temp.at(0) << endl;
	inFile.close();
	cout << endl;
	system("PAUSE");
	return 0;
}

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
    std::stringstream ss(s);
    std::string item;
    while(std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}


std::vector<std::string> split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    return split(s, delim, elems);
}

Try this one:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <iterator>
using namespace std;

vector<string> split(const string& src, const string& delim){	
	vector<string> result;
	string::size_type startPos = 0, endPos = 0;
	do{
		endPos = src.find_first_of(delim,startPos);
		string::size_type length = endPos - startPos;
		if(length != 0)
			result.push_back( src.substr(startPos,length) );
		startPos = endPos + 1;
	}while(endPos != string::npos);
	return result;
}
template<typename ForwardIterator>
void print(ForwardIterator begin, ForwardIterator end, const std::string& delim = " "){
	while(begin != end) cout << *begin++ << delim;
}
int main(){ 
	string whiteSpaces("\t\n ");
	vector<string> tokens = split("I\tmake no	gaurentess of|what|this,function.does",whiteSpaces + "|,.");
	print(tokens.begin(), tokens.end(),"\n");
}

Many thanks, worked :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.