查找字符串中某个字符的所有出现位置
我有需要从中提取值的逗号分隔字符串。问题是这些字符串永远不会是固定大小的。所以我决定遍历逗号组并阅读中间的内容。为了做到这一点,我创建了一个函数,它返回示例字符串中每个出现位置的位置。
这是一个聪明的方法吗?这被认为是坏代码吗?
#include <string>
#include <iostream>
#include <vector>
#include <Windows.h>
using namespace std;
vector<int> findLocation(string sample, char findIt);
int main()
{
string test = "19,,112456.0,a,34656";
char findIt = ',';
vector<int> results = findLocation(test,findIt);
return 0;
}
vector<int> findLocation(string sample, char findIt)
{
vector<int> characterLocations;
for(int i =0; i < sample.size(); i++)
if(sample[i] == findIt)
characterLocations.push_back(sample[i]);
return characterLocations;
}
I have comma delimited strings I need to pull values from. The problem is these strings will never be a fixed size. So I decided to iterate through the groups of commas and read what is in between. In order to do that I made a function that returns every occurrence's position in a sample string.
Is this a smart way to do it? Is this considered bad code?
#include <string>
#include <iostream>
#include <vector>
#include <Windows.h>
using namespace std;
vector<int> findLocation(string sample, char findIt);
int main()
{
string test = "19,,112456.0,a,34656";
char findIt = ',';
vector<int> results = findLocation(test,findIt);
return 0;
}
vector<int> findLocation(string sample, char findIt)
{
vector<int> characterLocations;
for(int i =0; i < sample.size(); i++)
if(sample[i] == findIt)
characterLocations.push_back(sample[i]);
return characterLocations;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如目前所写,这将简单地返回一个向量,其中包含字符本身的 int 表示,而不是它们的位置,如果我正确地阅读了你的问题,这就是你真正想要的。
替换这一行:
与这一行:
这应该给你你想要的向量。
As currently written, this will simply return a vector containing the int representations of the characters themselves, not their positions, which is what you really want, if I read your question correctly.
Replace this line:
with this line:
And that should give you the vector you want.
如果我正在审查这个,我会看到这个并假设你真正想做的是标记一个字符串,并且已经有很好的方法可以做到这一点。
我见过的最好的方法是使用
boost::分词器
。它允许您指定如何分隔字符串,然后为您提供一个很好的迭代器接口来迭代每个值。输出:
编辑 如果您不想依赖 boost,您还可以将
getline
与istringstream
一起使用,如 这个答案。从该答案中复制一些:输出:
这可能不是您所要求的,但我认为它解决了您试图解决的整体问题。
If I were reviewing this, I would see this and assume that what you're really trying to do is tokenize a string, and there's already good ways to do that.
Best way I've seen to do this is with
boost::tokenizer
. It lets you specify how the string is delimited and then gives you a nice iterator interface to iterate through each value.Output:
Edit If you don't want a dependency on boost, you can also use
getline
with anistringstream
as in this answer. To copy somewhat from that answer:Output:
This may not be directly what you're asking but I think it gets at your overall problem you're trying to solve.
我看起来也不错,一条评论是变量和类型的命名。当您真正推回字符本身(类型为
char
)时,您调用要返回characterLocations
的向量,该向量的类型为int
) 不是它的位置。我不确定更大的应用程序的用途是什么,但我认为传回位置会更有意义。或者做一个更加千篇一律的字符串标记化。Looks good to me too, one comment is with the naming of your variables and types. You call the vector you are going to return
characterLocations
which is of typeint
when really you are pushing back the character itself (which is typechar
) not its location. I am not sure what the greater application is for, but I think it would make more sense to pass back the locations. Or do a more cookie cutter string tokenize.好吧,如果您的目的是找到出现次数的索引,那么下面的代码将会更有效,就像在 C++ 中将对象作为参数一样,会导致对象被复制,这是不安全的,而且效率也较低。特别是在这种情况下返回向量是最糟糕的做法,这就是为什么将其作为参数引用给出会更好。
Well if your purpose is to find the indices of occurrences the following code will be more efficient as in c++ giving objects as parameters causes the objects to be copied which is insecure and also less efficient. Especially returning a vector is the worst possible practice in this case that's why giving it as a argument reference will be much better.
它的智能程度还取决于您对那些用逗号分隔的子字符串的处理方式。在某些情况下,避免搜索和分割并同时解析和处理字符串可能会更好(例如更快,内存要求更小),可能使用状态机。
How smart it is also depends on what you do with those subtstrings delimited with commas. In some cases it may be better (e.g. faster, with smaller memory requirements) to avoid searching and splitting and just parse and process the string at the same time, possibly using a state machine.