I used " using namespace std
;" in my entire study in C++,so basically I don't understand something like std::out,please help me out.Let's say I have a code shown below,i want the two string to be the same when I compare them.
int main(void)
{
using namespace std;
char a[10] = "123 ";
char b[10] = "123";
if(strcmp(a,b)==0)
{cout << "same";}
return 0;
}
use regex \\\\s+
to match all space characters and use regex_replace
to remove it
#include <iostream>
#include <regex>
#include <string>
int main()
{
std::string text = "Quick brown fox";
std::regex spaces("\\s+");
// construct a string holding the results
std::string result = std::regex_replace(text, spaces, "");
std::cout << '\n' << text << '\n';
std::cout << '\n' << result << '\n';
}
reference : http://en.cppreference.com/w/cpp/regex/regex_replace
如果您使用 std::string 而不是 char 您可以使用 boost 中的截断函数。
Use std::string
to do it
std::string a("123 ");
std::string b("123");
a.erase(std::remove_if(a.begin(), a.end(), ::isspace), a.end());
if (a == b)
std::cout << "Same";
The difference made by using
will be
using namespace std;
string a("123 ");
string b("123");
a.erase(remove_if(a.begin(), a.end(), ::isspace), a.end());
if (a == b)
cout << "Same";
It is generally advised not to use the using namespace std
. Don't forget to include <string>
and <algorithm>
.
EDIT If you still want to do it the C way, use the function from this post
https://stackoverflow.com/a/1726321/2425366
void RemoveSpaces(char * source) {
char * i = source, * j = source;
while (*j != 0) {
*i = *j++;
if (*i != ' ') i++;
}
*i = 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.