C++ C++;词表;在矢量/地图中找到完整的描述
如果你创建了一个单词列表,我正在做一个代码。它包含一个“单词”和一个“描述”。单词和描述有自己的向量。我也在用地图做同样的尝试 在我尝试查找单词之前,程序一直运行良好。程序将只从描述中提取最后一个单词。有没有办法把整个句子变成一个向量 这是我如何写下描述的代码。整个程序代码非常长,因此我只提及重要的内容:C++ C++;词表;在矢量/地图中找到完整的描述,c++,windows,vector,map,C++,Windows,Vector,Map,如果你创建了一个单词列表,我正在做一个代码。它包含一个“单词”和一个“描述”。单词和描述有自己的向量。我也在用地图做同样的尝试 在我尝试查找单词之前,程序一直运行良好。程序将只从描述中提取最后一个单词。有没有办法把整个句子变成一个向量 这是我如何写下描述的代码。整个程序代码非常长,因此我只提及重要的内容: cout<< "Describe your word:"; //Describtion by using vectors cin>> desc; //H
cout<< "Describe your word:"; //Describtion by using vectors
cin>> desc; //Here you enter the decribtion
getline(cin, desc); //So you can have "space" and write a whole sentence.
d.push_back(desc); //Place the describe at the back of the list so it is at the same index as the matching word
cout>desc//在这里输入描述
getline(cin,desc)//所以你可以用“空格”写一个完整的句子。
d、 推回(描述)//将“描述”放在列表的后面,使其与匹配的单词位于同一索引中
这是用来显示单词和描述的代码:
cout<< "Enter a word to lookup:";
cin>> word;
if (find(o.begin(), o.end(), word) !=o.end()) //Lookup if word excist in vector
{
int pos = find(o.begin(), o.end(), word) - o.begin(); //Searches which index the word is in the vector
cout<< "Describtion for " << word << " is " << d[pos] << endl; //d[pos] takes the description vector that is in the same index as the word vector
}
else
cout<< "Word not found! Try something else." << endl; //If word not found
cout>word;
if(查找(o.begin(),o.end(),word)!=o.end())//查找向量中的if单词
{
int pos=find(o.begin(),o.end(),word)-o.begin();//搜索单词在向量中的索引
cout如果从std::cin
中提取单个std::string
,它将只得到一个单词。首先,您将获得描述的第一个单词并将其放入desc
:
cin >> desc; // Here you enter the decribtion
然后,您将获得描述中的剩余单词,并将它们放入desc
。覆盖desc
的先前内容(第一个单词):
<> >现在,代码> DESC/<代码>包含了描述的第一个字。请考虑使用调试器找到这类东西。
其他一些建议:避免搜索向量两次。将find
的结果存储在变量中:
auto search = find(o.begin(), o.end(), word);
if (search != o.end()) {
int pos = std::distance(o.begin(), search);
// Use pos ...
}
你测试过的描述都有两个词吗?好的,谢谢你指出,它看起来只跳过了第一个词。添加cout现在我明白了,输入不包括第一个词。它与“getline(cin,desc)”有关吗?我想说,这与之前的那句话有更多的关系——读第一个单词的那句话。
getline(cin, desc); // So you can have "space" and write a whole sentence.
auto search = find(o.begin(), o.end(), word);
if (search != o.end()) {
int pos = std::distance(o.begin(), search);
// Use pos ...
}