如何查找字符数组的大小? 如何在C++中查找char数组的长度?我已经尝试了两种方法,但它们都导致数组中的字符数错误。到目前为止,我使用了strlen和sizeof操作符,但都没有用 void countOccurences(char *str, string word) { char *p; string t = "true"; string f = "false"; vector<string> a; p = strtok(str, " "); while (p != NULL) { a.push_back(p); p = strtok(NULL, " "); } int c = 0; for (int i = 0; i < a.size(); i++) { if (word == a[i]) { c++; } } int length = sizeof(str); //This is where I'm having the problem string result; cout << length << "\n"; if (length % 2 != 0) { if (c % 2 == 0) { result = "False"; } else { result = "True"; } } else { if (c % 2 == 0) { result = "True"; } else { result = "False"; } } if (strlen(str) != 0) { cout << result; } } int boolean() { char str[1000]; cin.getline(str, sizeof(str)); string word = "not"; countOccurences(str, word); return 0; } void countoccurrences(字符*str,字符串字) { char*p; 字符串t=“true”; 字符串f=“false”; 载体a; p=strtok(str,“”); while(p!=NULL) { a、 推回(p); p=strtok(空,“”); } int c=0; 对于(int i=0;i代码> Strutok>代码>并保存其返回值为变量。 < p>您可以找到一个现代C++解决方案: #include <iostream> #include <string_view> #include <string> #include <type_traits> template<typename String> inline std::size_t StrLength(String&& str) { using PureString = std::remove_reference_t<std::remove_const_t<String>>; if constexpr(std::is_same_v<char, PureString>){ return 1; } else if constexpr(std::is_same_v<char*, PureString>){ return strlen(str); } else{ return str.length(); } } template< typename String, typename Lambda, typename Delim = char > void ForEachWord(String&& str, Lambda&& lambda, Delim&& delim = ' ') { using PureStr = std::remove_reference_t<std::remove_reference_t<String>>; using View = std::basic_string_view<typename PureStr::value_type>; auto start = 0; auto view = View(str); while(true) { auto wordEndPos = view.find_first_of(delim, start); auto word = view.substr(start, wordEndPos-start); if (word.length() > 0){ lambda(word); } if (wordEndPos == PureStr::npos) { break; } start = wordEndPos + StrLength(delim); } } int main() { std::string text = "This is not a good sentence."; auto cnt = 0; ForEachWord( text, [&](auto word) { //some code for every word... like counting or printing if (word == "not" ){ ++cnt; } }, ' ' ); std::cout << cnt << "\n"; } #包括 #包括 #包括 #包括 样板 内联标准::大小标准长度(字符串和字符串) { 使用PureString=std::remove\u reference\t; 如果constexpr(std::is_same_v){ 返回1; } 其他的 如果constexpr(std::is_same_v){ 返回strlen(str); } 否则{ 返回str.length(); } } 模板< typename字符串, 类型名Lambda, typename Delim=char > void ForEachWord(String&&str,Lambda&&Lambda,Delim&&Delim='') { 使用PureStr=std::删除\u引用\u t; 使用View=std::basic\u string\u视图; 自动启动=0; 自动查看=查看(str); while(true) { auto-wordEndPos=view.find\u first\u of(delim,start); auto word=view.substr(开始,wordEndPos开始); if(word.length()>0){ lambda(单词); } if(wordEndPos==PureStr::npos) { 打破 } 开始=单词结束位置+字符串长度(delim); } } int main(){ std::string text=“这不是一个好句子。”; 自动cnt=0; 前置词( 文本 [&](自动字) { //每个单词都有一些代码…比如计数或打印 如果(单词==“不”){ ++碳纳米管; } }, ' ' ); std::cout char数组还是C-string?有区别。如果一个C-string和strlen()不起作用,那么你已经搞砸了。如果你丢失了常规数组的大小,那么你已经搞砸了。@sweenish我说的是字符数组。strtok修改字符串。你希望得到什么值?不再存在的原始字符串的大小?std::size()可以告诉你一个数组的大小,如果它没有衰减到仅仅是一个指针。顺便问一下,你为什么要使用C样式的数组而不是std::vector或std::array呢?你想在“countoccurrences”中做什么你是对的,但是使用C风格的字符串/数组是不好的。你应该避免使用这些函数。此外,你不应该使用Strutk。更快和更安全的是STD::String。VIEW。@贝尔恩德,为什么使用C风格的字符串/数组在C++中是坏的?我对语言比较陌生,而且我没有遇到过很多问题。far。是的,因为c字符串只是指向某些数据的指针。您会丢失字符串结尾处的信息。这是在c字符串中处理的,最后一个“零”。strtok只是为了与c兼容。此函数的问题是它修改了数据。好的,您可以在标记化之前复制它……但有更智能的解决方案(如果你喜欢,我可以发布一些东西)

如何查找字符数组的大小? 如何在C++中查找char数组的长度?我已经尝试了两种方法,但它们都导致数组中的字符数错误。到目前为止,我使用了strlen和sizeof操作符,但都没有用 void countOccurences(char *str, string word) { char *p; string t = "true"; string f = "false"; vector<string> a; p = strtok(str, " "); while (p != NULL) { a.push_back(p); p = strtok(NULL, " "); } int c = 0; for (int i = 0; i < a.size(); i++) { if (word == a[i]) { c++; } } int length = sizeof(str); //This is where I'm having the problem string result; cout << length << "\n"; if (length % 2 != 0) { if (c % 2 == 0) { result = "False"; } else { result = "True"; } } else { if (c % 2 == 0) { result = "True"; } else { result = "False"; } } if (strlen(str) != 0) { cout << result; } } int boolean() { char str[1000]; cin.getline(str, sizeof(str)); string word = "not"; countOccurences(str, word); return 0; } void countoccurrences(字符*str,字符串字) { char*p; 字符串t=“true”; 字符串f=“false”; 载体a; p=strtok(str,“”); while(p!=NULL) { a、 推回(p); p=strtok(空,“”); } int c=0; 对于(int i=0;i代码> Strutok>代码>并保存其返回值为变量。 < p>您可以找到一个现代C++解决方案: #include <iostream> #include <string_view> #include <string> #include <type_traits> template<typename String> inline std::size_t StrLength(String&& str) { using PureString = std::remove_reference_t<std::remove_const_t<String>>; if constexpr(std::is_same_v<char, PureString>){ return 1; } else if constexpr(std::is_same_v<char*, PureString>){ return strlen(str); } else{ return str.length(); } } template< typename String, typename Lambda, typename Delim = char > void ForEachWord(String&& str, Lambda&& lambda, Delim&& delim = ' ') { using PureStr = std::remove_reference_t<std::remove_reference_t<String>>; using View = std::basic_string_view<typename PureStr::value_type>; auto start = 0; auto view = View(str); while(true) { auto wordEndPos = view.find_first_of(delim, start); auto word = view.substr(start, wordEndPos-start); if (word.length() > 0){ lambda(word); } if (wordEndPos == PureStr::npos) { break; } start = wordEndPos + StrLength(delim); } } int main() { std::string text = "This is not a good sentence."; auto cnt = 0; ForEachWord( text, [&](auto word) { //some code for every word... like counting or printing if (word == "not" ){ ++cnt; } }, ' ' ); std::cout << cnt << "\n"; } #包括 #包括 #包括 #包括 样板 内联标准::大小标准长度(字符串和字符串) { 使用PureString=std::remove\u reference\t; 如果constexpr(std::is_same_v){ 返回1; } 其他的 如果constexpr(std::is_same_v){ 返回strlen(str); } 否则{ 返回str.length(); } } 模板< typename字符串, 类型名Lambda, typename Delim=char > void ForEachWord(String&&str,Lambda&&Lambda,Delim&&Delim='') { 使用PureStr=std::删除\u引用\u t; 使用View=std::basic\u string\u视图; 自动启动=0; 自动查看=查看(str); while(true) { auto-wordEndPos=view.find\u first\u of(delim,start); auto word=view.substr(开始,wordEndPos开始); if(word.length()>0){ lambda(单词); } if(wordEndPos==PureStr::npos) { 打破 } 开始=单词结束位置+字符串长度(delim); } } int main(){ std::string text=“这不是一个好句子。”; 自动cnt=0; 前置词( 文本 [&](自动字) { //每个单词都有一些代码…比如计数或打印 如果(单词==“不”){ ++碳纳米管; } }, ' ' ); std::cout char数组还是C-string?有区别。如果一个C-string和strlen()不起作用,那么你已经搞砸了。如果你丢失了常规数组的大小,那么你已经搞砸了。@sweenish我说的是字符数组。strtok修改字符串。你希望得到什么值?不再存在的原始字符串的大小?std::size()可以告诉你一个数组的大小,如果它没有衰减到仅仅是一个指针。顺便问一下,你为什么要使用C样式的数组而不是std::vector或std::array呢?你想在“countoccurrences”中做什么你是对的,但是使用C风格的字符串/数组是不好的。你应该避免使用这些函数。此外,你不应该使用Strutk。更快和更安全的是STD::String。VIEW。@贝尔恩德,为什么使用C风格的字符串/数组在C++中是坏的?我对语言比较陌生,而且我没有遇到过很多问题。far。是的,因为c字符串只是指向某些数据的指针。您会丢失字符串结尾处的信息。这是在c字符串中处理的,最后一个“零”。strtok只是为了与c兼容。此函数的问题是它修改了数据。好的,您可以在标记化之前复制它……但有更智能的解决方案(如果你喜欢,我可以发布一些东西),c++,arrays,char,sizeof,C++,Arrays,Char,Sizeof,字符串的结尾是字符'\0'检查该字符以停止搜索。sizeof(str)是错误的。它提供指针的大小(str是指针),这是一个固定的数字,通常是4或8,具体取决于您的平台 std::strlen(str)是正确的,但是strtok在您尝试获取大小之前,会在数组中插入一组\0。strlen将在第一个\0处停止,并给出它前面的字符数 调用 STRLUN >代码> Strutok>代码>并保存其返回值为变量。 < p>您可以找到一个现代C++解决方案: #include <iostream>

字符串的结尾是字符'\0'检查该字符以停止搜索。

sizeof(str)
是错误的。它提供指针的大小(
str
是指针),这是一个固定的数字,通常是
4
8
,具体取决于您的平台

std::strlen(str)
是正确的,但是
strtok
在您尝试获取大小之前,会在数组中插入一组
\0
strlen
将在第一个
\0
处停止,并给出它前面的字符数

调用<代码> STRLUN <代码> >代码> Strutok>代码>并保存其返回值为变量。

< p>您可以找到一个现代C++解决方案:

#include <iostream>
#include <string_view>
#include <string>
#include <type_traits>

template<typename String>
inline std::size_t StrLength(String&& str)
{
    using PureString = std::remove_reference_t<std::remove_const_t<String>>;
    if constexpr(std::is_same_v<char, PureString>){
        return 1;
    }
    else 
    if constexpr(std::is_same_v<char*, PureString>){
        return strlen(str);
    }
    else{
        return str.length();
    }
}

template<
    typename String,
    typename Lambda,
    typename Delim = char
>
void ForEachWord(String&& str, Lambda&& lambda, Delim&& delim = ' ')
{
    using PureStr = std::remove_reference_t<std::remove_reference_t<String>>;
    using View = std::basic_string_view<typename PureStr::value_type>;

    auto start = 0;
    auto view = View(str);

    while(true)
    {
        auto wordEndPos = view.find_first_of(delim, start);
        auto word =  view.substr(start, wordEndPos-start);

        if (word.length() > 0){
            lambda(word);
        }
        
        if (wordEndPos == PureStr::npos)
        {
            break;
        }
        start = wordEndPos + StrLength(delim);
    }
}

int main() {
   std::string text = "This is not a good sentence.";
   auto cnt = 0;
   ForEachWord(
       text, 
       [&](auto word)
       {
          //some code for every word... like counting or printing
          if (word == "not" ){
             ++cnt;
          }
       },
       ' '
   );
   std::cout << cnt << "\n";
}
#包括
#包括
#包括
#包括
样板
内联标准::大小标准长度(字符串和字符串)
{
使用PureString=std::remove\u reference\t;
如果constexpr(std::is_same_v){
返回1;
}
其他的
如果constexpr(std::is_same_v){
返回strlen(str);
}
否则{
返回str.length();
}
}
模板<
typename字符串,
类型名Lambda,
typename Delim=char
>
void ForEachWord(String&&str,Lambda&&Lambda,Delim&&Delim='')
{
使用PureStr=std::删除\u引用\u t;
使用View=std::basic\u string\u视图;
自动启动=0;
自动查看=查看(str);
while(true)
{
auto-wordEndPos=view.find\u first\u of(delim,start);
auto word=view.substr(开始,wordEndPos开始);
if(word.length()>0){
lambda(单词);
}
if(wordEndPos==PureStr::npos)
{
打破
}
开始=单词结束位置+字符串长度(delim);
}
}
int main(){
std::string text=“这不是一个好句子。”;
自动cnt=0;
前置词(
文本
[&](自动字)
{
//每个单词都有一些代码…比如计数或打印
如果(单词==“不”){
++碳纳米管;
}
},
' '
);

std::cout char数组还是C-string?有区别。如果一个C-string和
strlen()
不起作用,那么你已经搞砸了。如果你丢失了常规数组的大小,那么你已经搞砸了。@sweenish我说的是字符数组。
strtok
修改字符串。你希望得到什么值?不再存在的原始字符串的大小?
std::size()
可以告诉你一个数组的大小,如果它没有衰减到仅仅是一个指针。顺便问一下,你为什么要使用C样式的数组而不是
std::vector
std::array
呢?你想在“countoccurrences”中做什么你是对的,但是使用C风格的字符串/数组是不好的。你应该避免使用这些函数。此外,你不应该使用Strutk。更快和更安全的是STD::String。VIEW。@贝尔恩德,为什么使用C风格的字符串/数组在C++中是坏的?我对语言比较陌生,而且我没有遇到过很多问题。far。是的,因为c字符串只是指向某些数据的指针。您会丢失字符串结尾处的信息。这是在c字符串中处理的,最后一个“零”。strtok只是为了与c兼容。此函数的问题是它修改了数据。好的,您可以在标记化之前复制它……但有更智能的解决方案(如果你喜欢,我可以发布一些东西)