如何将"查找"和"substr"函数与向量一起使用?

How to use the 'find' and 'substr' functions with Vectors?

本文关键字:一起 向量 substr 查找 函数      更新时间:2023-10-16

我正在制作一个程序,该程序搜索字符串向量的第一词句子。它将第一个单词存储在新向量中以供以后使用。在代码中,alldords是读取文件后的结果向量。

allwords contents:

0000000    f   i   r   s   t       l   i   n   e       i   s       h   e
0000020    r   e   .  n   s   e   c   o   n   d       l   i   n   e    
0000040    i   s       h   e   r   e       a   f   t   e   r       t   h
0000060    e       f   i   r   s   t   ?  n   t   h   i   r   d       l
0000100    i   n   e       c   o   m   e   s       n   e   x   t   !    
0000120   n   f   o   u   r   t   h       l   i   n   e       t   h   e
0000140    n       t   h   e  n   .   .   .   f   i   f   t   h       i
0000160    s       t   h   e       l   i   n   e       b   e   f   o   r
0000200    e       t   h   e       s   i   x   t   h  n   s   i   x   t
0000220    h       i   s       t   h   e       l   a   s   t       o   n

然后我的代码获取第一个单词。

string firstWord = allWords.substr(0, allWords.find(' '));
someVector.push_back(firstWord);

在这里,我试图获取第一个句子的第一个单词,但是遇到了错误:

‘类STD :: vector>’没有成员名为" find"

'类STD :: vector>’没有成员名为" subStr"

我查找了很多代码,其他人似乎至少可以使用向量使用"查找"。关于如何在不创建我自己的发现和基本功能的情况下解决此问题的任何建议?

您可以编写一个循环以穿越向量。在循环主体内,您可以在当前字符串上操作。如果您穿过整个向量,则将为向量中的每个字符串重复这些操作。您可以使用字符串的成员函数findsubstr