C++ 从字符串读取(可能的数组用法?)C++;
我试图在这里编写alil函数,它基本上是从字符串读取的。它每隔三个字符读取一次,并使用前置条件(if语句)对其求值。如果符合条件,它将用新的三个字母替换这三个字母。然后它将输出新字符串 我试着写代码,但似乎不能得到正确的逻辑。程序运行,但不打印任何内容。不要介意函数名和不精确性。我只是在做一个示例函数来测试一下C++ 从字符串读取(可能的数组用法?)C++;,c++,C++,我试图在这里编写alil函数,它基本上是从字符串读取的。它每隔三个字符读取一次,并使用前置条件(if语句)对其求值。如果符合条件,它将用新的三个字母替换这三个字母。然后它将输出新字符串 我试着写代码,但似乎不能得到正确的逻辑。程序运行,但不打印任何内容。不要介意函数名和不精确性。我只是在做一个示例函数来测试一下 string amino_acids(string line) { string acid; string acids; string newline;
string amino_acids(string line)
{
string acid;
string acids;
string newline;
for( int i= 0; i < line.length(); i++)
{
acid = line[i];
}
for (int i = 0; i < 3; i++)
{
acids = acid[i];
if(acids == "GUU")
{
acids = "ZAP";
}
newline = acids;
}
cout << "Acids: " <<newline <<endl;
return newline;
}
字符串氨基酸(字符串行)
{
串酸;
串酸;
字符串换行符;
对于(int i=0;i cout使用[]
运算符对std::string
进行索引会产生一个char
,对于该char,字符串恰好有一个重载的运算符=
即使您按照我相信您的意图循环(正如对问题的评论所提到的,您可能不是),因为ACIS(取单个字符的值)永远不会等于您将其与之进行比较的三个字符串。因此,不会执行任何替换
要做您想做的事情,请尝试以下操作:
for (int i = 0; i + 3 < line.length(); i += 3) // counting by 3 until end of line
{
if (line.substr(i, 3) == "GUU") // if the substring matches
{
line.assign("ZAP", i, 3); // overwrite it with new substring
}
}
return line;
for(int i=0;i+3
只有最后一个作业才有持久的影响。如果你真的需要在acid中输入三个字符,你可能想使用+=
在acid
中添加字符,而不是=
。但是,如果你像这样循环所有的行,你最终会做acid=line;
。我想你想要一些更像是acid=line.substr(0,3)
更一般地说,试着在代码中放一些std::cout
语句,这样你就知道变量的实际值…你很容易就会发现它们不是你所期望的值。或者,使用交互式调试器,观察每个语句执行的影响。阅读你的描述,你会发现蚂蚁之类的
//note below does not compile, its just psuedo-code
string amino_acid(const string& sequence){
string result = sequence; //make copy of original sequence
For i = 0 to sequence.length - 3
string next3Seq = sequence(i,3); //grab next 3 character from current index
If next3Seq == 'GUU' //if the next next three sequence is 'GUU'
then result.replace(i,3,'ZAP'); //replace 'GUU' with 'ZAP'
EndIf
EndFor
return result;
}
你可以用它来开始编码。祝你好运。根据我对你问题的理解。我已经写了一些代码。请看下面
string acids;
string newLine;
int limit=1;
for(int i=0;i<line.length();i++)
{
acids=acids+line[i];
if(limit==3)//Every 3 characters
{
if(acids == "GUU")
{
acids = "ZAP";
}
limit=1;
acids=""
newline=newline+acids;
}
limit++;
return newline;
}
string;
字符串换行符;
整数极限=1;
for(int i=0;i)你的大括号放错地方了。我想你是说第二个for
循环在第一个循环内,除其他外。for(int i=0;i
等同于if(!line.empty())acid=line.back()这就是你想要的吗?
for
循环中的if
似乎没有任何意义。你为什么要重复if
测试3次?是的,我只是对自己的代码感到困惑lol@user2188311,对自己的代码感到困惑是一种非常有趣的感觉,不是吗?这是我们通常努力避免的,但是回到旧代码中尤其可以做到这一点。好吧,也许索引字符串是个坏主意。但是,难道没有一个函数可以读取字符串中的一定数量的字符吗?
for (int i = 0; i < 3; i++)
{
acids = acid[i];
int main()
try
{
...your code in here...
some_string.at(i);
}
catch (const std::exception& e)
{
std::cerr << "caught exception: " << e.what() << '\n';
}
//note below does not compile, its just psuedo-code
string amino_acid(const string& sequence){
string result = sequence; //make copy of original sequence
For i = 0 to sequence.length - 3
string next3Seq = sequence(i,3); //grab next 3 character from current index
If next3Seq == 'GUU' //if the next next three sequence is 'GUU'
then result.replace(i,3,'ZAP'); //replace 'GUU' with 'ZAP'
EndIf
EndFor
return result;
}
string acids;
string newLine;
int limit=1;
for(int i=0;i<line.length();i++)
{
acids=acids+line[i];
if(limit==3)//Every 3 characters
{
if(acids == "GUU")
{
acids = "ZAP";
}
limit=1;
acids=""
newline=newline+acids;
}
limit++;
return newline;
}