This below function will produce output as {"a","b","c"} as vector for the string input "a=b+c" when delimiter is "+="
void Split( const std::string &str, std::vector<std::string> & tokens , std::string delim)
{
boost::split(tokens, str, boost::is_any_of(delim));
}
I need output as {"a=b+c"}
Please suggest for modifications or any suitable functions available
This is the code you're looking for, regular split only takes chars so you have to use split_regex instead:
#include <string>
#include <iostream>
#include <iterator>
#include <boost/regex.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <vector>
int main() {
std::string str = "a=b+c" ;
std::vector<std::string> result;
boost::algorithm::split_regex(result, str, boost::regex("\\+="));
}
Note you'll have to link it to boost_regex with -lboost_regex otherwise it'll fire errors at you.
Also in the string "\\+=" the reason why there are two backslashes is that regular expressions use '+' as a special character so then you have to use a '\' character to escape that usage, but one '\' turns it into a special character '\+' so you must escape the first '\' like so: '\\+='
Related
Currently going thru a c++ course.
I had to create a word cipher using the strings: alphabet and key.
to cipher an inputted word with less code as possible I created this solution that gives the error:
no matching function for call to std::basic_string<char>::find(std::string&, int&, int)
I don't know how to solve it, neither do I know if my idea would work at all, would LOVE some help.
Thanks for your attention :)
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main() {
string alphabet {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
string key {"XZNLWEBGJHQDYVTKFUOMPCIASRxznlwebgjhqdyvtkfuompciasr"};
string word_to_encrypt {};
getline (cin,word_to_encrypt);
for (int i=0;i<word_to_encrypt.size;i++){
word_to_encrypt.replace (i,1,key,(alphabet.find(word_to_encrypt,i,1)),1);
}
cout<< word_to_encrypt;
}
Two problems:
First size is a function and not a variable. Therefore you need size().
Secondly std::string::find() has no overload which takes a std::string and two ints: https://en.cppreference.com/w/cpp/string/basic_string/find , but you can use the overload which takes a CharT instead by adding .c_str() or .data().
This compiles at least:
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main() {
string alphabet {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"};
string key {"XZNLWEBGJHQDYVTKFUOMPCIASRxznlwebgjhqdyvtkfuompciasr"};
string word_to_encrypt {};
getline (cin,word_to_encrypt);
for (int i=0;i<word_to_encrypt.size();i++){
word_to_encrypt.replace(i, 1, key, (
alphabet.find(word_to_encrypt.c_str(), i, 1)),1);
}
cout<< word_to_encrypt;
}
I have the following code:
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/assign/list_of.hpp>
#include <string>
#include <vector>
int main()
{
std::vector<char> some_vec = boost::assign::list_of('1')('2')('3')('4')('5')('\0')('\0');
std::string str(some_vec.begin(), some_vec.end());
boost::trim_right_if(str, boost::is_any_of("\0"));
}
I think that in str should be "12345", but there's "12345\0\0". Why and how can I solve it?
This code works
boost::trim_right_if(str, boost::is_any_of(boost::as_array("\0") );
The trick is to use boost::as_array
I do not know this functions boost::is_any_of but the fact that its argument is a string literal it seems it considers "\0" as an empty set of characters (en empty string literal). So the algorithm trims nothing.
It is only my supposition.
Is there any alternative to QStringList in Boost or STL. What I want to achieve is to split path eg. dvb://1.2.3.4/launchpad/dyn/index.htm to separate strings as it can by done simply in QString List:
QStringList path = objectPath.split(QChar('/'), QString::SkipEmptyParts);
Thank You.
boost::split can split a string into a std::vector<std::string>, based on one or multiple delimiters:
#include <vector>
#include <string>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>
std::vector<std::string> path_parts;
std::string s("some/file/path");
boost::split(path_parts, s, boost::is_any_of("/"));
A .csv file is written like this:
M9005U00-X30A0S00-1;BAS;X;-0.002;-0.095
S707RY00-X30AOS00-1;HMV;X;+0.002;+0.081
W3005U00-X30BOJ00-1;BAS;X;+0.026;-0.138
H307QZ00-X30BOJ00-1;HMV;X;-0.025;+0.122
....
now I want to create a function, i.e.
double find_and_extract (string sss)
when this function is used with a keyword as its parameter, for example
find_and_extract (W3005U00-X30BOJ00-1);
it will search in the .csv file line by line, find corresponding line (in this case it should be the third line), and extract the certin part "+0.026" in this line, return as a double.
How should I write this function?
edit: Here is the code i've written so far:
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <vector>
#include <iterator>
#include <cstdlib>
#include <cstdio>
#include <sstream>
#include <stdlib.h>
using namespace std;
void main()
{
find_and_extract (W3005U00-X30BOJ00-1);
}
double find_and_extract (string sss)
{
vector<string> vecarray;
ifstream infile("C:\\Data\\testdata.csv");
string temppo;
string contnt;
char csv_extract[40];
stringstream ss;
vector <string>::iterator ptr;
while (!infile.eof())
{
infile.getline(csv_extract,40);
ss << csv_extract;
ss >> contnt;
vecarray.push_back(contnt);
}
for (ptr=vecarray.begin();ptr!=vecarray.end();ptr++)
{
if ((*ptr).find(sss)==0)
temppo = (*ptr).substr(27,6);
}
return (strtod(temppo.c_str(),NULL,0));
}
Could anyone help me out to point out the errors?
Seeing as you already have the file as a string, I'd use the Knuth–Morris–Pratt algorithm to find the key, find the position of the 3rd and 4th semi-colons on that line and return the string in between them.
That's just an outline - you'll need to add error handling.
Check out strtok(). This is actually a pretty trivial task, and should be a good learning project if you are still new to C++.
You could use sed: This way, you could search for the key very efficiently, without having to implement an algorithm yourself. When you found the key, you can let sed output the parts of the line you need (use regular expressions to describe the pattern and groupings to print only part of it). After that, it's a simple string to float conversion that can be done in a programming language of your choice.
For starters:
sed -n 's/RegexToMatchYourKeyAndValues/MatchedValues/p'
If the text lines in the file are the same length, you may want to read the lines as blocks (i.e. many lines == 1 block) into a buffer, then search the buffer.
Your performance bottleneck will the reading the data from the file. In general, the search method you choose will be faster than reading in the data.
I want to replace all the occurances of ' in a string to ^, but i saw string.replace is not the right function for me, do I need to write my own? It's boring.
You can use std::replace from <algorithm> instead of using string::replace from <string>
Sample code
#include <iostream>
#include <algorithm>
int main()
{
std::string s = "I am a string";
std::replace(s.begin(),s.end(),' ',',');
std::cout<< s;
}
Output : I,am,a,string