parse inFile line by line while using regex to execute function - c++

I sort of painted myself in a corner and need some guidance. I'm doing some parsing using regex while reading from an infstream. What I want to do is
while(getLine(inFile, str)) {
search(str) for regex match
if(match)
str = my_function(str)
outFile << str
else
outFile unchanged str
}
As it stands, I'm doing this:
while(getLine(inFile, str)) {
auto des = std::sregex_iterator(str.cbegin(), str.cend(), dest);
auto reg_it = std::sregex_iterator();
std::for_each(des , reg_it, [](std::smatch const& m){
str = my_function(str)
outFile << str
});
}
Unfortunately, this doesn't let me edit the file and write it back out in order, as the way I'm doing it only gives me access to the returned matches.
Any guidance would be greatly appreciated!

This works:
if (std::regex_match(str.cbegin(), str.cend(), matches , my_regex)) {
string baz;
baz = my_function(str); // we have a match
outFile baz;
}
else outFile << str << std::endl;

Would this work?
while(getLine(inFile, str)) {
auto des = std::sregex_iterator(str.cbegin(), str.cend(), dest);
auto reg_it = std::sregex_iterator();
int matches = 0;
std::for_each(des , reg_it, [](std::smatch const& m)){
str = my_function(str);
outFile << str;
matches++;
});
if (matches == 0) {
outFile << str;
}
}

Related

How can I trim empty/whitespace lines?

I have to process badly mismanaged text with creative indentation. I want to remove the empty (or whitespace) lines at the beginning and end of my text without touching anything else; meaning that if the first or last actual lines respectively begin or end with whitespace, these will stay.
For example, this:
<lines, empty or with whitespaces ...>
<text, maybe preceded by whitespace>
<lines with or without text...>
<text, maybe followed by whitespace>
<lines, empty or with whitespaces ...>
turns to
<text, maybe preceded by whitespace>
<lines with or without text...>
<text, maybe followed by whitespace>
preserving the spaces at the beginning and the end of the actual text lines (the text might also be entirely whitespace)
A regex replacing (\A\s*(\r\n|\Z)|\r\n\s*\Z) by emptiness does exactly what I want, but regex is kind of overkill, and I fear it might cost me some time when processing texts with a lot of lines but not much to trim.
On the other hand, an explicit algorithm is easy to make (just read until a non-whitespace/the end while remembering the last line feed, then truncate, and do the same backwards) but it feels like I'm missing something obvious.
How can I do this?
As you can see from this discussion, trimming whitespace requires a lot of work in C++. This should definitely be included in the standard library.
Anyway, I've checked how to do it as simply as possible, but nothing comes near the compactness of RegEx. For speed, it's a different story.
In the following you can find three versions of a program which does the required task. With regex, with std functions and with just a couple of indexes. The last one can be also made faster because you can avoid copying altogether, but I left it for fair comparison:
#include <string>
#include <sstream>
#include <chrono>
#include <iostream>
#include <regex>
#include <exception>
struct perf {
std::chrono::steady_clock::time_point start_;
perf() : start_(std::chrono::steady_clock::now()) {}
double elapsed() const {
auto stop = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed_seconds = stop - start_;
return elapsed_seconds.count();
}
};
std::string Generate(size_t line_len, size_t empty, size_t nonempty) {
std::string es(line_len, ' ');
es += '\n';
for (size_t i = 0; i < empty; ++i) {
es += es;
}
std::string nes(line_len - 1, ' ');
es += "a\n";
for (size_t i = 0; i < nonempty; ++i) {
nes += nes;
}
return es + nes + es;
}
int main()
{
std::string test;
//test = " \n\t\n \n \tTEST\n\tTEST\n\t\t\n TEST\t\n \t\n \n ";
std::cout << "Generating...";
std::cout.flush();
test = Generate(1000, 8, 10);
std::cout << " done." << std::endl;
std::cout << "Test 1...";
std::cout.flush();
perf p1;
std::string out1;
std::regex re(R"(^\s*\n|\n\s*$)");
try {
out1 = std::regex_replace(test, re, "");
}
catch (std::exception& e) {
std::cout << e.what() << std::endl;
}
std::cout << " done. Elapsed time: " << p1.elapsed() << "s" << std::endl;
std::cout << "Test 2...";
std::cout.flush();
perf p2;
std::stringstream is(test);
std::string line;
while (std::getline(is, line) && line.find_first_not_of(" \t\n\v\f\r") == std::string::npos);
std::string out2 = line;
size_t end = out2.size();
while (std::getline(is, line)) {
out2 += '\n';
out2 += line;
if (line.find_first_not_of(" \t\n\v\f\r") != std::string::npos) {
end = out2.size();
}
}
out2.resize(end);
std::cout << " done. Elapsed time: " << p2.elapsed() << "s" << std::endl;
if (out1 == out2) {
std::cout << "out1 == out2\n";
}
else {
std::cout << "out1 != out2\n";
}
std::cout << "Test 3...";
std::cout.flush();
perf p3;
static bool whitespace_table[] = {
1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
};
size_t sfl = 0; // Start of first line
for (size_t i = 0, end = test.size(); i < end; ++i) {
if (test[i] == '\n') {
sfl = i + 1;
}
else if (whitespace_table[(unsigned char)test[i]]) {
break;
}
}
size_t ell = test.size(); // End of last line
for (size_t i = test.size(); i-- > 0;) {
if (test[i] == '\n') {
ell = i;
}
else if (whitespace_table[(unsigned char)test[i]]) {
break;
}
}
std::string out3 = test.substr(sfl, ell - sfl);
std::cout << " done. Elapsed time: " << p3.elapsed() << "s" << std::endl;
if (out1 == out3) {
std::cout << "out1 == out3\n";
}
else {
std::cout << "out1 != out3\n";
}
return 0;
}
Running it on C++ Shell you get these timings:
Generating... done.
Test 1... done. Elapsed time: 4.2288s
Test 2... done. Elapsed time: 0.0077323s
out1 == out2
Test 3... done. Elapsed time: 0.000695783s
out1 == out3
If performance is important, it's better to really test it with the real files.
As a side note, this regex doesn't work on MSVC, because I couldn't find a way of avoiding ^ and $ to match the start and end of lines, that is disable the multiline mode of operation. If you run this, it throws an exception saying regex_error(error_complexity): The complexity of an attempted match against a regular expression exceeded a pre-set level.
I think I'll ask how to cope with this!
If whitespace in front of the first line or after the last non-whitespace-only line can be removed then this answer https://stackoverflow.com/a/217605/14258355 will suffice.
However, due to this constraint and if you do not want to use regex, I would propose to convert the string into lines and then build the string back up again from the first to the last non-whitespace-only line.
Here is a working example: https://godbolt.org/z/rozxj6saj
Convert the string to lines:
std::vector<std::string> StringToLines(const std::string &s) {
// Create vector with lines (not using input stream to keep line break
// characters)
std::vector<std::string> result;
std::string line;
for (auto c : s) {
line.push_back(c);
// Check for line break
if (c == '\n' || c == '\r') {
result.push_back(line);
line.clear();
}
}
// add last bit
result.push_back(line);
return result;
}
Build the string from the first to the last non-whitespace-only line:
bool IsNonWhiteSpaceString(const std::string &s) {
return s.end() != std::find_if(s.begin(), s.end(), [](unsigned char uc) {
return !std::isspace(uc);
});
}
std::string TrimVectorEmptyEndsIntoString(const std::vector<std::string> &v) {
std::string result;
// Find first non-whitespace line
auto it_begin = std::find_if(v.begin(), v.end(), [](const std::string &s) {
return IsNonWhiteSpaceString(s);
});
// Find last non-whitespace line
auto it_end = std::find_if(v.rbegin(), v.rend(), [](const std::string &s) {
return IsNonWhiteSpaceString(s);
});
// Build the string
for (auto it = it_begin; it != it_end.base(); std::advance(it, 1)) {
result.append(*it);
}
return result;
}
Usage example:
// Create a test string
std::string test_string(
" \n\t\n \n TEST\n\tTEST\n\t\tTEST\n TEST\t\n \t");
// Output result
std::cout << TrimVectorEmptyEndsIntoString(StringToLines(test_string));
Output showing whitespace:

C++ alternative of Java's split(str, -1) [duplicate]

What would be easiest method to split a string using c++11?
I've seen the method used by this post, but I feel that there ought to be a less verbose way of doing it using the new standard.
Edit: I would like to have a vector<string> as a result and be able to delimitate on a single character.
std::regex_token_iterator performs generic tokenization based on a regex. It may or may not be overkill for doing simple splitting on a single character, but it works and is not too verbose:
std::vector<std::string> split(const string& input, const string& regex) {
// passing -1 as the submatch index parameter performs splitting
std::regex re(regex);
std::sregex_token_iterator
first{input.begin(), input.end(), re, -1},
last;
return {first, last};
}
Here is a (maybe less verbose) way to split string (based on the post you mentioned).
#include <string>
#include <sstream>
#include <vector>
std::vector<std::string> split(const std::string &s, char delim) {
std::stringstream ss(s);
std::string item;
std::vector<std::string> elems;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
// elems.push_back(std::move(item)); // if C++11 (based on comment from #mchiasson)
}
return elems;
}
Here's an example of splitting a string and populating a vector with the extracted elements using boost.
#include <boost/algorithm/string.hpp>
std::string my_input("A,B,EE");
std::vector<std::string> results;
boost::algorithm::split(results, my_input, boost::is_any_of(","));
assert(results[0] == "A");
assert(results[1] == "B");
assert(results[2] == "EE");
Another regex solution inspired by other answers but hopefully shorter and easier to read:
std::string s{"String to split here, and here, and here,..."};
std::regex regex{R"([\s,]+)"}; // split on space and comma
std::sregex_token_iterator it{s.begin(), s.end(), regex, -1};
std::vector<std::string> words{it, {}};
I don't know if this is less verbose, but it might be easier to grok for those more seasoned in dynamic languages such as javascript. The only C++11 features it uses is auto and range-based for loop.
#include <string>
#include <cctype>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
string s = "hello how are you won't you tell me your name";
vector<string> tokens;
string token;
for (const auto& c: s) {
if (!isspace(c))
token += c;
else {
if (token.length()) tokens.push_back(token);
token.clear();
}
}
if (token.length()) tokens.push_back(token);
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
vector<string> split(const string& str, int delimiter(int) = ::isspace){
vector<string> result;
auto e=str.end();
auto i=str.begin();
while(i!=e){
i=find_if_not(i,e, delimiter);
if(i==e) break;
auto j=find_if(i,e, delimiter);
result.push_back(string(i,j));
i=j;
}
return result;
}
int main(){
string line;
getline(cin,line);
vector<string> result = split(line);
for(auto s: result){
cout<<s<<endl;
}
}
My choice is boost::tokenizer but I didn't have any heavy tasks and test with huge data.
Example from boost doc with lambda modification:
#include <iostream>
#include <boost/tokenizer.hpp>
#include <string>
#include <vector>
int main()
{
using namespace std;
using namespace boost;
string s = "This is, a test";
vector<string> v;
tokenizer<> tok(s);
for_each (tok.begin(), tok.end(), [&v](const string & s) { v.push_back(s); } );
// result 4 items: 1)This 2)is 3)a 4)test
return 0;
}
This is my answer. Verbose, readable and efficient.
std::vector<std::string> tokenize(const std::string& s, char c) {
auto end = s.cend();
auto start = end;
std::vector<std::string> v;
for( auto it = s.cbegin(); it != end; ++it ) {
if( *it != c ) {
if( start == end )
start = it;
continue;
}
if( start != end ) {
v.emplace_back(start, it);
start = end;
}
}
if( start != end )
v.emplace_back(start, end);
return v;
}
#include <string>
#include <vector>
#include <sstream>
inline vector<string> split(const string& s) {
vector<string> result;
istringstream iss(s);
for (string w; iss >> w; )
result.push_back(w);
return result;
}
Here is a C++11 solution that uses only std::string::find(). The delimiter can be any number of characters long. Parsed tokens are output via an output iterator, which is typically a std::back_inserter in my code.
I have not tested this with UTF-8, but I expect it should work as long as the input and delimiter are both valid UTF-8 strings.
#include <string>
template<class Iter>
Iter splitStrings(const std::string &s, const std::string &delim, Iter out)
{
if (delim.empty()) {
*out++ = s;
return out;
}
size_t a = 0, b = s.find(delim);
for ( ; b != std::string::npos;
a = b + delim.length(), b = s.find(delim, a))
{
*out++ = std::move(s.substr(a, b - a));
}
*out++ = std::move(s.substr(a, s.length() - a));
return out;
}
Some test cases:
void test()
{
std::vector<std::string> out;
size_t counter;
std::cout << "Empty input:" << std::endl;
out.clear();
splitStrings("", ",", std::back_inserter(out));
counter = 0;
for (auto i = out.begin(); i != out.end(); ++i, ++counter) {
std::cout << counter << ": " << *i << std::endl;
}
std::cout << "Non-empty input, empty delimiter:" << std::endl;
out.clear();
splitStrings("Hello, world!", "", std::back_inserter(out));
counter = 0;
for (auto i = out.begin(); i != out.end(); ++i, ++counter) {
std::cout << counter << ": " << *i << std::endl;
}
std::cout << "Non-empty input, non-empty delimiter"
", no delimiter in string:" << std::endl;
out.clear();
splitStrings("abxycdxyxydefxya", "xyz", std::back_inserter(out));
counter = 0;
for (auto i = out.begin(); i != out.end(); ++i, ++counter) {
std::cout << counter << ": " << *i << std::endl;
}
std::cout << "Non-empty input, non-empty delimiter"
", delimiter exists string:" << std::endl;
out.clear();
splitStrings("abxycdxy!!xydefxya", "xy", std::back_inserter(out));
counter = 0;
for (auto i = out.begin(); i != out.end(); ++i, ++counter) {
std::cout << counter << ": " << *i << std::endl;
}
std::cout << "Non-empty input, non-empty delimiter"
", delimiter exists string"
", input contains blank token:" << std::endl;
out.clear();
splitStrings("abxycdxyxydefxya", "xy", std::back_inserter(out));
counter = 0;
for (auto i = out.begin(); i != out.end(); ++i, ++counter) {
std::cout << counter << ": " << *i << std::endl;
}
std::cout << "Non-empty input, non-empty delimiter"
", delimiter exists string"
", nothing after last delimiter:" << std::endl;
out.clear();
splitStrings("abxycdxyxydefxy", "xy", std::back_inserter(out));
counter = 0;
for (auto i = out.begin(); i != out.end(); ++i, ++counter) {
std::cout << counter << ": " << *i << std::endl;
}
std::cout << "Non-empty input, non-empty delimiter"
", only delimiter exists string:" << std::endl;
out.clear();
splitStrings("xy", "xy", std::back_inserter(out));
counter = 0;
for (auto i = out.begin(); i != out.end(); ++i, ++counter) {
std::cout << counter << ": " << *i << std::endl;
}
}
Expected output:
Empty input:
0:
Non-empty input, empty delimiter:
0: Hello, world!
Non-empty input, non-empty delimiter, no delimiter in string:
0: abxycdxyxydefxya
Non-empty input, non-empty delimiter, delimiter exists string:
0: ab
1: cd
2: !!
3: def
4: a
Non-empty input, non-empty delimiter, delimiter exists string, input contains blank token:
0: ab
1: cd
2:
3: def
4: a
Non-empty input, non-empty delimiter, delimiter exists string, nothing after last delimiter:
0: ab
1: cd
2:
3: def
4:
Non-empty input, non-empty delimiter, only delimiter exists string:
0:
1:
One possible way of doing this is finding all occurrences of the split string and storing locations to a list. Then count input string characters and when you get to a position where there is a 'search hit' in the position list then you jump forward by 'length of the split string'. This approach takes a split string of any length. Here is my tested and working solution.
#include <iostream>
#include <string>
#include <list>
#include <vector>
using namespace std;
vector<string> Split(string input_string, string search_string)
{
list<int> search_hit_list;
vector<string> word_list;
size_t search_position, search_start = 0;
// Find start positions of every substring occurence and store positions to a hit list.
while ( (search_position = input_string.find(search_string, search_start) ) != string::npos) {
search_hit_list.push_back(search_position);
search_start = search_position + search_string.size();
}
// Iterate through hit list and reconstruct substring start and length positions
int character_counter = 0;
int start, length;
for (auto hit_position : search_hit_list) {
// Skip over substrings we are splitting with. This also skips over repeating substrings.
if (character_counter == hit_position) {
character_counter = character_counter + search_string.size();
continue;
}
start = character_counter;
character_counter = hit_position;
length = character_counter - start;
word_list.push_back(input_string.substr(start, length));
character_counter = character_counter + search_string.size();
}
// If the search string is not found in the input string, then return the whole input_string.
if (word_list.size() == 0) {
word_list.push_back(input_string);
return word_list;
}
// The last substring might be still be unprocessed, get it.
if (character_counter < input_string.size()) {
word_list.push_back(input_string.substr(character_counter, input_string.size() - character_counter));
}
return word_list;
}
int main() {
vector<string> word_list;
string search_string = " ";
// search_string = "the";
string text = "thetheThis is some text to test with the split-thethe function.";
word_list = Split(text, search_string);
for (auto item : word_list) {
cout << "'" << item << "'" << endl;
}
cout << endl;
}

How to split the string vector array in C++

How to split the string vector in C++?
The input string values are read from the file, and it's format is like below.
P00 ARRIVAL:3 CPU:1 I/O:3 CPU:7 I/O:1 CPU:3
P01 ARRIVAL:2 CPU:9
P02 ARRIVAL:0 CPU:6
P03 ARRIVAL:4 CPU:1
P04 ARRIVAL:0 CPU:5
However, I need just the value like,
p00 3 1 3 7 1 3
p01 2 9
p02 0 6
...
And this is my part of code. The file is read by a line by line, and each line is saved on string vector array.
vector<string> procList; // file
void readProc(char *filename) {
std::ifstream file(filename);
std::string str;
while (std::getline(file, str))
{
procList.push_back(str);
}
file.close();
for (int i = 0, size = procList.size(); i < size; ++i) {
_tprintf(TEXT("[%s] \n"), procList[i]);
}
}
Thanks.
Using regexps for parsing:
live
#include <regex>
std::vector<std::string> example = {
"P00 ARRIVAL:3 CPU:1 I/O:3 CPU:7 I/O:1 CPU:3",
"P01 ARRIVAL:2 CPU:9",
"P02 ARRIVAL:0 CPU:6",
"P03 ARRIVAL:4 CPU:1",
"P04 ARRIVAL:0 CPU:5"
};
std::regex re{"[\\w\\d]+:(\\d+)"};
for (auto s : example) {
std::cout << "\n";
std::regex_token_iterator<std::string::iterator> it{s.begin(), s.end(), re, 1};
decltype(it) end{};
while (it != end) std::cout << *it++ << ":";
}
will output:
3:1:3:7:1:3:
2:9:
0:6:
4:1:
0:5:
With regex_token_iterator you could also specify to iterate over name and value in your data. You specify in regexp two groups (using parentheses), and then specify in constructor to regex_token_iterator which groups (sub matches) to include during iteration using: {1, 2}.
live
std::regex re{R"(([\w\d/]+):(\d+))"};
for (auto s : example) {
std::cout << "\n";
using reg_itr = std::regex_token_iterator<std::string::iterator>;
for (reg_itr it{s.begin(), s.end(), re, {1,2}}, end{}; it != end;) {
std::cout << *it++ << ":"; std::cout << std::stoi(*it++) << " ";
}
}
will output:
ARRIVAL:3 CPU:1 I/O:3 CPU:7 I/O:1 CPU:3
ARRIVAL:2 CPU:9
ARRIVAL:0 CPU:6
ARRIVAL:4 CPU:1
ARRIVAL:0 CPU:5
Assuming the format is id {key:value}. The following code utilizes stringstream and getline. Might have problems, I haven't tested.
vector<string> procList; // file
void readProc(char *filename) {
std::ifstream file(filename);
std::string str;
while (std::getline(file, str))
{
std::stringstream in(str);
std::stringstream out;
std::string temp;
in>>temp;
out<<temp;
while(getline(in, temp, ':')) {
in>>temp;
out<<"\t"<<temp;
}
procList.push_back(out.str());
}
file.close();
for (int i = 0, size = procList.size(); i < size; ++i) {
_tprintf(TEXT("[%s] \n"), procList[i]);
}
}

Regex expression not vaild

Im facing difficulties getting my regex to work. Im trying to only export the urls from the string. Heres some of the text that's in the string. pastebin.com/wA9N1Gbi . The regex expression that Im trying to use is
(?< protocol>https?:\/\/)(?:(?< urlroot>[^\/?#\n\s]+))?(?< urlResource>[^?#\n\s]+)?(?< queryString>\?(?:[^#\n\s]*))?(?:#(?< fragment>[^\n\s]))?
HereĀ“s a link regex101.com/r/bH1eS9/3
Not working unfortunately, When compiling I get following error "Unhandled exception at 0x7638DAE8 in Historik.exe: Microsoft C++ exception: std::regex_error at memory location 0x0018ED9C.". Does anyone of you have another idea how I could do it? Is there another regex function that might be better for this task?
The coding im having at this moment. Thanks in advance.
string str;
std::ifstream in("c:/Users/Petrus/Documents/History", std::ios::binary);
std::stringstream buffer;
buffer << in.rdbuf();
std::string contents(buffer.str())
unsigned counter = 0;
std::regex word_regex(
R"((?<protocol>https?:\/\/)(?:(?<urlroot>[^\/?#\n\s]+))?(?<urlResource>[^?#\n\s]+)?(?<queryString>\?(?:[^#\n\s]*))?(?:#(?<fragment>[^\n\s]))?)",
std::regex::extended
);
auto words_begin = std::sregex_iterator(contents.begin(), contents.end(), word_regex);
auto words_end = std::sregex_iterator();
for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
std::smatch match = *i;
std::string match_str = match.str();
for (const auto& res : match) {
counter++;
std::cout << counter++ << ": " << res << std::endl;
}
Do you need such a complex regular expression? Can you get away with something less rigorous?
std::string load_file(const std::string& filename)
{
std::ostringstream oss;
if(auto ifs = std::ifstream(filename, std::ios::binary))
oss << ifs.rdbuf();
else
throw std::runtime_error("Failed to open file: " + filename);
return oss.str();
}
int main(int, const char* const*)
{
std::string s = load_file("test.txt");
// crude... but effective?
std::regex e(R"(https?:\/\/[^/]+[[:print:][:punct:]]*)");
auto itr = std::sregex_iterator(s.begin(), s.end(), e);
auto end = std::sregex_iterator();
unsigned counter = 0;
for(; itr != end; ++itr)
std::cout << ++counter << ": " << itr->str(0) << '\n';
}
Output:
1: http://boplats.vaxjo.se/
2: http://192.168.0.7/
3: http://old.honeynet.org/
4: http://old.honeynet.org/scans/scan15/som/som11.txt
5: http://en.hackdig.com/
6: http://parallelrecovery.com/pdf-password.html
7: http://digitalcorpora.org/corp
8: http://tv4play.se/program/nyhetsmorgon
9: http://bredbandskollen.se/
10: http://194.47.149.19/dv1482/Lab5/
...

inserting on the vector c++

The code below has alot of outputting strings at the end i try to push the back onto avector and the append it to a string so then i can return it, but it only gets the last string which is outputted i need to get all of them.
What am i doign wrong so that i can push back all the strings
DCS_LOG_DEBUG("--------------- Validating .X/ ---------------")
std::string str = el[i].substr(3);
std::vector<std::string>st;
split(st,str,boost::is_any_of("/"));
boost::regex const string_matcher(splitMask[0]);
if(boost::regex_match(st[0],string_matcher))
{
a = "Correct Security Instruction\n";
}
else
{
a = "Incorrect Security Instruction\n"
}
boost::regex const string_matcher4(splitMask[4]);
if(boost::regex_match(st[4],string_matcher4))
{
a = "Correct Autograpgh\n"
}
else
{
a = "Incorrect Autograpgh\n"
}
boost::regex const string_matcher5(splitMask[5]);
if(boost::regex_match(st[5],string_matcher5))
{
a = "Correct Free text\n";
}
else
{
a = "Incorrect Free text\n"
}
std::vector<std::string>::iterator it;
std::string s = ("");
output.push_back(a);
i++;
for(it = output.begin(); it < output.end(); it++)
{
s+= *it;
}
return s;
Assigning more than once to a will replace, not concatenate. What you are looking for, is more likely output streaming (or output iterators).
Propose to simplify:
DCS_LOG_DEBUG("--------------- Validating .X/ ---------------")
std::string str = el[i].substr(3);
std::vector<std::string> st;
split(st,str,boost::is_any_of("/"));
boost::regex const string_matcher(splitMask[0]);
boost::regex const string_matcher4(splitMask[4]);
boost::regex const string_matcher5(splitMask[5]);
std::ostringstream oss;
oss << (boost::regex_match(st[0],string_matcher )? "correct":"incorrect") << " Security Instruction\n";
oss << (boost::regex_match(st[4],string_matcher4)? "correct":"incorrect") << " Autograpgh\n";
oss << (boost::regex_match(st[5],string_matcher5)? "correct":"incorrect") << " Free text\n";
return oss.str();
Include <sstream> for std::ostringstream
Are you sure that the result you are getting is not just the first string?
That said, it is not entirely clear what you are trying to do, but assuming that there is some looping code above what you posted it seems that your problem is with the positioning of the for loop.
while( i < el.size() ) //Assuming something like this
{
...
else
{
a = "Incorrect Free text\n"
}
output.push_back(a);
i++;
}
//move this stuff out of the loop so that it only runs after you have
// processed all the strings in el
std::vector<std::string>::iterator it;
std::string s = ("");
for(it = output.begin(); it < output.end(); it++)
{
s+= *it;
}
return s;
}