Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string analysis

Tags:

c++

string

IF a string may include several un-necessary elements, e.g., such as @, #, $,%.

How to find them and delete them?

I know this requires a loop iteration, but I do not know how to represent sth such as @, #, $,%.

If you can give me a code example, then I will be really appreciated.

like image 611
user297850 Avatar asked Aug 20 '10 13:08

user297850


People also ask

What is a STRING analysis?

STRING is a database of known and predicted protein-protein interactions. The interactions include direct (physical) and indirect (functional) associations; they stem from computational prediction, from knowledge transfer between organisms, and from interactions aggregated from other (primary) databases.

What is STRING software used for?

STRING is the knowledgebase and software tool for known and predicted protein-protein interactions. It includes direct (physical) and indirect (functional) associations derived from various sources, such as genomic context, high-throughput experiments, (conserved) co-expression and the literature.

Why do we use STRING in database?

The STRING database aims to integrate all known and predicted associations between proteins, including both physical interactions as well as functional associations.


1 Answers

The usual standard C++ approach would be the erase/remove idiom:

#include <string>
#include <algorithm>
#include <iostream>
struct OneOf {
        std::string chars;
        OneOf(const std::string& s) : chars(s) {}
        bool operator()(char c) const {
                return chars.find_first_of(c) != std::string::npos;
        }
};
int main()
{
    std::string s = "string with @, #, $, %";
    s.erase(remove_if(s.begin(), s.end(), OneOf("@#$%")), s.end());
    std::cout << s << '\n';
}

and yes, boost offers some neat ways to write it shorter, for example using boost::erase_all_regex

#include <string>
#include <iostream>
#include <boost/algorithm/string/regex.hpp>
int main()
{
    std::string s = "string with @, #, $, %";
    erase_all_regex(s, boost::regex("[@#$%]"));
    std::cout << s << '\n';
}
like image 103
Cubbi Avatar answered Nov 07 '22 16:11

Cubbi