I am trying to write the code that can searches the string for the first character that matches any of the characters specified in its arguments.
The recommended function for me was c++ compare function, but the exam given in this website is :
Ref: cplusplus.com
// comparing apples with apples
#include <iostream>
#include <string>
int main ()
{
std::string str1 ("green apple");
std::string str2 ("red apple");
if (str1.compare(str2) != 0)
std::cout << str1 << " is not " << str2 << '\n';
if (str1.compare(6,5,"apple") == 0)
std::cout << "still, " << str1 << " is an apple\n";
if (str2.compare(str2.size()-5,5,"apple") == 0)
std::cout << "and " << str2 << " is also an apple\n";
if (str1.compare(6,5,str2,4,5) == 0)
std::cout << "therefore, both are apples\n";
return 0;
}
Below is part of my code and i would like to use any function (like compare) to find the matched character like "1NCAGAATTTGCATCATGAACGATGAGCTGATCGTGANGNN" from input txt or .fastq file.
I have written this code for dna search.
part of my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
ifstream read_stream(read_files.at(i).c_str());
read_stream.seekg(read_sizes[my_rank-1]);
//checking the position in file;
file_pos = 0;
if(my_rank != comm_sz -1){
while(file_pos < read_sizes[my_rank]){
read_stream >> read_temp;
//Get position in input sequence
file_pos = read_stream.tellg();
//write the code to searches the string for the first character that matches any of the characters specified in its arguments.
// charachters for comparision would be: "1NCAGAATTTGCATCATGAACGATGAGCTGATCGTGANGNN"
{
code::
}
}
}
I have tried find and .first of before as below but didn't work. I talked to my supervisor and he told me i can use compare function.
1 2 3 4 5 6 7 8
if(read_temp.size() == 40 && std::string::npos == read_temp.find_first_of("@1NCAGAATTTGCATCATGAACGATGAGCTGATCGTGANGNN")){
found = read_map.find (read_temp); // only reading and checking
if (found == read_map.end()){
read_map[read_temp] = 1; // if it could not found in the map
}else{ // if it is found then:
read_map[found->first]++;
}
}
good call my 2nd loop probably needed a break once found. It also should probably be bool instead of int and set true instead of ++. My headache is better and brain is coming back online. Minor but better. Mine won't work on Unicode, though, the string versions can be set to do that.
a for loop tied to find is O(N^2). I believe mine was just N. I couldn't find a way to use a built in string function without increasing the work done. But I would love to see it if someone figures that out.
I think it's possible to do what you're trying but try no to be rush. Those examples might lead you code to suffer from a lot of bugs and errors that are going to be hard to detect later. There are programs that might help you with it, as checkmarx but I recommend you to make sure you code well and detect them on your own.
Good luck.
@gunnerfunner and @jonnin
Thank you all for you comments and your help. It really helped me to understand to how to do this function. I really appreciate your time.