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.
Related
I'm trying to print the text "Ääkkösiä ruutuun." to console with c++. I have windows 7 and am using Code::Blocks editor. Searching on the subject I found that maybe these sort of lines would help
_setmode(_fileno(stdout), _O_U16TEXT);
wstring s{L"Ääkkösiä ruutuun."};
wcout<<s<<endl;
But when I try to compile it, I get the error: _fileno was not declared in this scope.
I have all these includes:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stdexcept>
#include <cmath>
#include <sstream>
#include <fstream>
#include <codecvt>
#include <locale>
#include <fcntl.h>
#include <io.h>
#include <stdio.h>
#include <cstdio>
#include <ostream>
what am I missing?
Also, one other thing I tried was locale, but then locale::empty wasn't found! Why doesn't my c++ have anything in it?
EDIT
Here is a picture of what my program is doing now.
It prints out just the first letter (Ä). What happens to the rest?
Ok, it seems that setmode sets it so that only one letter gets printed. (Even trying to print normal texts with multiple commands, just results in a single letter.) Without it the scandinavian letters don't print correctly, thought. They look like this:
The answer you found is for Visual Studio, not Code::Blocks.
While the C standard specifies what should in in <stdio.h>, it only specifies a minimum. Implementors may add their own functions, and should do so using an _ (underscore prefix). This is why you should NOT use that prefix. You don't know what you'll break. Microsoft clearly signaled their non-standard extensions using the correct prefix.
The answer is tagged C++, but C++ inherits the contents of <stdio.h> from C.
The line
setlocale(LC_CTYPE, ".OCP");
works!
A complete example:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
wstring readFile(const char* filename) {
wifstream wif(filename);
locale myLoc("");
//locale utf8_locale(locale(), new gel::stdx::utf8cvt<true>);
wif.imbue(myLoc);
basic_stringstream<wchar_t> wss;
wss << wif.rdbuf();
return wss.str();
}
int main() {
setlocale(LC_CTYPE, ".OCP");
wstring contents = readFile("test.txt");
wcout<<L"Does anything get printed out at all???"<<endl;
//wcout <<contents<<endl;
wstring s{L"Ääkkösiä ruutuun."};
wcout<<s<<endl;
wcout<<L"Näkyykö äkköset?"<<endl;
return 0;
}
The text read from file (utf-8) still doesn't print correctly, though.
It should be
Hei!
Täällä on kaksi riviä.
ä's go awry there.
Output:
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: '\\+='
I'm trying to use std::getline, but my compiler is telling me that getline isn't identified?
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <fstream>
#include <cstdlib>
int main(){
using namespace std;
string line;
ifstream ifile("test.in");
if(ifile.is_open()){
while(ifile.good()){
getline(ifile,line);
}
}
}
std::getline is defined in the string header.
#include <string>
Also, your code isn't using anything from cstring, cstdio, cmath, or cstdlib; why bother including these?
EDIT: To clarify the confusion regarding the cstring and string headers, cstring pulls the contents of the C runtime library's string.h into the std namespace; string is part of the C++ standard library and contains getline, std::basic_string<> (and its specializations std::string and std::wstring), etc. -- two very different headers.
As ildjarn points out, the function is declared in <string>, and I'm suprised you didn't get an error at:
string line;
Also, this:
while(ifile.good()){
getline(ifile,line);
}
is not the way to write a read loop. You MUST test the success of the read operation, not the current stream state. You want:
while( getline(ifile,line) ) {
}
this is happening because getline comes from the string library, you need to #include <string> or #include <cstring>
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
does this work on string in c++?
string s="lomi";
cout<<s<<endl;
what is bad in this code?
#include <iostream>
#include <cstring>
using namespace std;
int main(){
string s=string("lomi");
for (int i=0;i<s.length();i++){
s[i]= s[i]+3;
}
std::cout<<s<<std::endl;
return 0;
}
?
Yes.
(after you have #included the corresponding headers, and using the std namespace, etc.)
Edit: What's wrong with your code is you should
#include <string>
instead of
#include <cstring>
cstring is C's string.h header, which defines functions like strlen, strcpy, etc. that manipulates a C string, i.e. char*.
string defines C++'s string class which you're using.
short answer: yes
long answer: string s = "lomi"works due to the string(const char*) constructor.
Works for me -- does it work for you?
Remember to do this first:
#include <ostream>
#include <string>
using namespace std;
yes, cout in C++ knows how to deal with strings
Yes it should compile and work, if you want to print "lomi\n", and you have included <string> and <ostream> and declared using namespace std;.