- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个将句子拆分为 2D 指针的系统。
我不想使用任何类型的库或其他方式(如字符串),因为我想练习指针并学习它们。
char** sscanf(char* hstring)
{
int count = 0;
char* current = hstring;
while (*current)
{
if (*current == ' ')
{
count++;
}
while (*current == ' ')
{
current++;
}
if (*current)
break;
current++;
}
char** result = new char*[count];
current = hstring;
char* nstr = new char;
int c = 0, i = 0;
while (*current)
{
if (!*current) break;
cout << "t1";
if (*current == ' ')
{
*(++result) = nstr;
nstr = nullptr;
nstr = new char;
}
cout << "t2";
while (*current != '/0' && *current == ' ')
{
current++;
}
cout << "t3";
while (*current != '/0' && *current != ' ')
{
if (!*current) break;
*(++nstr) = *current;
current++;
}
cout << "t4";
*nstr = '/0';
cout << "t5";
}
return result;
}
但这很奇怪,有时会将我重定向到
static size_t __CLRCALL_OR_CDECL length(_In_z_ const _Elem * const _First) _NOEXCEPT // strengthened
{ // find length of null-terminated string
return (_CSTD strlen(_First));
}
错误:Acces Violation,其他时候,选择一条随机线并将其命名为 Acces Breakout(对不起,如果我拼错了)
我想从你这里得到的不是简单地修复我的代码,我想要一些解释,因为我想学习这些东西。
最佳答案
我知道您将此功能作为练习,但作为 C++,我想警告您 new char*[count]
之类的东西是不良做法 这就是创建 std::vector
或 std::array
的原因。
您似乎对动态分配的工作原理感到困惑。 char* nstr = new char;
语句将只在堆内存中创建一个字节 (char),并且什么都没有保证与它相邻。这意味着 ++nstr
是一个“无效”操作,我的意思是,它使 nstr
指向分配的字节之后的下一个字节,这可能是一些随机无效的位置。
您的代码中还有很多其他危险操作,例如多次调用 new
(保留内存)而不调用 delete
当你不再使用保留内存时(又名内存泄漏)。话虽如此,我强烈建议你研究这个主题,例如从 ISO C++ FAQ on memory management 开始。 .
此外,在深入研究指针和动态分配之前,您应该更熟悉语句和流控制。我这样说是因为我看到了一些明显的误解,例如:
while (*current) {
if (!*current) break;
...
}
if
语句中的检查肯定是假的,因为 while
检查就在它之前执行并保证相反的条件为真。这意味着这个 if
永远不会被评估为 true,它完全没有用。
另一点是:不要将您的函数命名为与标准库相同的。 sscanf
已被采用,请选择另一个(更有意义的)。这将为您将来省去一些麻烦;用于正确命名您自己的函数。
我心情很好,所以我将在这里完成一些步骤。无论如何,如果有人正在寻找优化且准备就绪的解决方案,请参阅 Split a String in C++ .
阅读您的代码,我可以猜出您需要的一些步骤:
char** split_string(char* sentence)
{
// Count the number of words in the sentence
// Allocate memory for the answer (a 2D buffer)
// Write each word in the output
}
与其尝试一下子把它们都弄好,不如一个一个地尝试? (注意函数和参数的名称,在我看来更清晰)。
您可以从一个简单的 main() 开始,测试您的解决方案。这是我的(对不起,我不能只是适应你的)。对于那些优化上瘾的人来说,这不是一个优化的解决方案,而是一个简单的 OP 片段。
// I'll be using this header and namespace on the next snippets too.
#include <iostream>
using namespace std;
int main()
{
char sentence[] = " This is my sentence ";
int n_words = 0;
char *p = sentence;
bool was_space = true; // see logic below
// Reading the whole sentence
while (*p) {
// Check if it's a space and advance pointer
bool is_space = (*p++ == ' ');
if (was_space && !is_space)
n_words++; // count as word a 'rising edge'
was_space = is_space;
}
cout << n_words;
}
对其进行测试,确保您了解其工作原理。现在,您可以进入下一步。
好吧,你想为每个单词分配一个缓冲区,所以我们需要知道每个单词的大小(我不会讨论这是否是解决句子拆分问题的好方法......)。这不是在上一步中计算的,所以我们现在可能会这样做。
int main()
{
char sentence[] = " This is my sentence ";
///// Count the number of words in the sentence
int n_words = 0;
char *p = sentence;
bool was_space = true; // see logic below
// Reading the whole sentence
while (*p) {
// Check if it's a space and advance pointer
bool is_space = (*p++ == ' ');
if (was_space && !is_space)
n_words++; // count as word a 'rising edge'
was_space = is_space;
}
///// Allocate memory for the answer (a 2D buffer)
// This is more like C than C++, but you asked for it
char **words = new char*[n_words];
char *ini = sentence; // the initial char of each word
for (int i = 0; i < n_words; ++i) {
while (*ini == ' ') ini++; // search next non-space char
char *end = ini + 1; // pointer to the end of the word
while (*end && *end != ' ') end++; // search for \0 or space
int word_size = end - ini; // find out the word size by address offset
ini = end; // next for-loop iteration starts
// at the next word
words[i] = new char[word_size]; // a whole buffer for one word
cout << i << ": " << word_size << endl; // debugging
}
// Deleting it all, one buffer at a time
for (int i = 0; i < n_words; ++i) {
delete[] words[i]; // delete[] is the syntax to delete an array
}
}
请注意,我正在删除 main() 中分配的缓冲区。当您将此逻辑移动到您的函数时,此释放将由函数的调用者执行,因为它可能会在删除缓冲区之前使用缓冲区。
我想你明白了。分配单词并将逻辑移动到分离的函数。用 Minimal, Complete, and Verifiable example 更新您的问题如果您还有问题。
我知道这是一个问答论坛,但我认为这已经是对 OP 和可能通过此处的其他人的健康回答。如果我应该做出不同的回答,请告诉我。
关于c++ - 使用字符指针拆分单词中的句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43559794/
假设我有这个变量 var image = "image.jpg"; 我正在尝试拆分变量图像的内容并将 _thumbs 插入其中以获得类似 image_thumbs.jpg 的内容。 我该如何解决这个问
我有一个包含多个问题和答案的单元格,其组织方式类似于 CSV。因此,为了将所有这些问题和答案分开,使用逗号作为分隔符的简单拆分应该很容易分开。 不幸的是,有些值使用逗号作为小数分隔符。有没有办法避免这
这是简单的代码: import std.algorithm; import std.array; import std.file; void main(string[] args) { aut
我正在尝试解析一个看起来像的 txt 文件 A - 19 B - 2 C - 3 我正在使用扫描仪方法读取它并在“- ”中拆分,以便我的输出看起来像: A 19 B 2 C 3 但是它似乎没有正确拆分
我有这些网址字符串 file:///home/we/Pictures/neededWord/3193_n.jpg file:///home/smes/Pictures/neededWord/jds_2
我正在解析一个 CVS 文件,如下所示: "07555555555",25.70,18/11/2010,01/03/2011,N,133,0,36,,896,537,547,,Mr,John,Doe,
我在脚本中使用以下行返回 $folder 处所有文件夹的所有路径地点。 dir -recurse $folder|?{$_.PSIsContainer}|select -ExpandProperty
我正在尝试将字符串格式化为word+word+word 例如 “超音乐节”变成“超+音乐+节日” 我尝试过使用以下代码 query.split(" ").join("+"); 或 query.repl
我叫 luis,住在 arg。我有一个问题,无法解决。 **IN BASH** pwd /home/labs-perl ls file1.pl file2.pl **IN PERL** my $ls
我想从包 javax.json 中拆分 JsonArray,但我找不到完成这项工作的便捷方法。我查看了文档,只能想到迭代 JsonArray 并使用 JsonArrayBuilder 手动添加项目。
我希望在第一个 ':' 处拆分字符串,以防止字符串的第二部分包含 ':' 时出现问题。我一直在研究正则表达式,但仍然遇到一些问题,有人可以帮我吗?谢谢。 最佳答案 您可以使用overload of s
我想拆分列表的列表 ((A,1,2,3),(B,4,5,6),(C,7,8,9))进入: (A,1) (A,2) (A,3) (B,4) (B,5) ... 我试过rdd.flatMapValues(
我有一个文本文件,其中每一行都有数据。它看起来像这样: number0;text0 number1;text1 number2;text2 ..等等 所以我通过 xmlhttprequest 将该文本
问题很简单——比如说,我得到了函数,它接收数组作为参数 void calc(double[] data) 如何将这些数据“拆分”成两个子数组并像这样传递给子函数 calc_sub(data(0, le
我想显示来自 EMAIL_TEXT 数据库列的数据,在定义的字符处拆分列。出于某种原因,我的结果只打印第一行到我拆分字符串的位置,跳过其余行。这是我希望在每个“|”之后拆分的数据。 这里是要拆分的数据
我有一个动态数组,我想排除字符串的第一部分,但我不知道第一部分之后会有多少对象,我想将它们全部包含在一个新字符串中。 string = "text.'''hi''','''who''' '''are'
我想拆分 URL 的某些特定部分,这是我目前所做的。 var query = window.location.pathname.split( '/' ); query = window.locati
我有一条消息携带 XML(订单),其中包含多个同质节点(比如产品列表)以及其他信息(比如地址、客户详细信息等)。我必须使用另一个外部服务提供的详细信息来丰富每个“产品”,并返回带有丰富“产品”的相同完
我有一个动态生成的大字符串,我正在拆分它。 var myString="val1, val, val3, val4..... val400" 我对此字符串进行了简单的拆分, myString= myS
这个问题在这里已经有了答案: Java String split removed empty values (5 个答案) 关闭 7 年前。 我正在尝试使用 split(";") 将字符串转换为数组
我是一名优秀的程序员,十分优秀!