在 for 循环中更新两个变量时遇到问题C++
Trouble getting two variables to update in C++ for loop
我正在创建一个将句子拆分为单词的函数,并相信这样做的方法是使用str.substr
,从str[0]
开始,然后使用 str.find 查找第一个" "
字符的索引。 然后更新 str.find 的起始位置参数,从该" "
字符的索引开始,直到str.length()
结束。
我使用两个变量来标记单词的起始位置和结束位置,并用最后一个的结束位置更新起始位置变量。但是它没有像我目前那样在循环中按预期更新,并且无法弄清楚原因。
#include <iostream>
#include <string>
using namespace std;
void splitInWords(string str);
int main() {
string testString("This is a test string");
splitInWords(testString);
return 0;
}
void splitInWords(string str) {
int i;
int beginWord, endWord, tempWord;
string wordDelim = " ";
string testWord;
beginWord = 0;
for (i = 0; i < str.length(); i += 1) {
endWord = str.find(wordDelim, beginWord);
testWord = str.substr(beginWord, endWord);
beginWord = endWord;
cout << testWord << " ";
}
}
使用字符串流更容易。
#include <vector>
#include <string>
#include <sstream>
using namespace std;
vector<string> split(const string& s, char delimiter)
{
vector<string> tokens;
string token;
istringstream tokenStream(s);
while (getline(tokenStream, token, delimiter))
{
tokens.push_back(token);
}
return tokens;
}
int main() {
string testString("This is a test string");
vector<string> result=split(testString,' ');
return 0;
}
您可以使用现有的C++库编写它:
#include <string>
#include <vector>
#include <iterator>
#include <sstream>
int main()
{
std::string testString("This is a test string");
std::istringstream wordStream(testString);
std::vector<std::string> result(std::istream_iterator<std::string>{wordStream},
std::istream_iterator<std::string>{});
}
几个问题:
-
substr()
方法第二个参数是长度(不是位置(。// Here you are using `endWord` which is a poisition in the string. // This only works when beginWord is 0 // for all other values you are providing an incorrect len. testWord = str.substr(beginWord, endWord);
-
find()
方法从第二个参数中搜索。// If str[beginWord] contains one of the delimiter characters // Then it will return beginWord // i.e. you are not moving forward. endWord = str.find(wordDelim, beginWord); // So you end up stuck on the first space.
-
假设您修复了上述问题。您将在每个单词的前面添加空格。
// You need to actively search and remove the spaces // before reading the words.
你可以做的好事:
这里:
void splitInWords(string str) {
您正在按值传递参数。这意味着您正在制作副本。更好的技术是通过 const 引用传递(您没有修改原始或副本(。
void splitInWords(string const& str) {
另一种选择
您可以使用流功能。
void split(std::istream& stream)
{
std::string word;
stream >> word; // This drops leading space.
// Then reads characters into `word`
// until a "white space" character is
// found.
// Note: it emptys words before adding any
}
相关文章:
- 你好。。。id_public变量不应该给出结果为 81 和 86 吗?为什么它为两个派生类占用不同的内存位置?
- 我可以创建一个包含两个变量的 for 循环,但时间复杂度仍然为 O(n) 吗?
- 如何更好地检查两个 char 变量是否在一组值中?
- 如何将两个字符串加在一起,就好像它们是变量一样?
- 命令行参数,cant 或两个变量
- 在 for 循环中更新两个变量时遇到问题C++
- 为什么具有静态存储持续时间的同一内联变量在包含在 VS2017 编译的两个翻译单元中时会构造和销毁两次
- C++ 编译时在两个变量之间交替
- C++ 如何在包含两个变量的结构中存储与变量不同数量的值?
- 如何比较两个tm(来自ctime)变量
- 两个不同的进程,在同一地址上有 2 个 std::atomic 变量?
- 如何在 c++ 中将两个不同类型的变量分配给一个变量
- 为什么我不能使用最小和最大这两个词作为变量名称?
- 为什么一个接一个声明的两个变量在内存中不相邻?
- 在链接到两个 exe 的 dll 中共享全局变量
- C++ 两个线程,共享几个整数变量
- 为什么动态分配的两个变量的内存位置不是连续的?
- 计算两个uint8_t变量之间差值的最快方法是什么?
- 如何使用C++中不是文字的变量在数字中显示单引号和两个引号?假设 6'2" 英尺
- 是否可以在C++中使用宏交换两个变量的出现?