小编Tun*_*izz的帖子

重新定义; 不同的基本类型(typedef struct)

当在不同的文件中定义结构时,我在尝试使结构正常工作时遇到了一些麻烦.据我所知,错误告诉我结构被定义了两个不同的时间.我相信也许我可能需要在某处使用extern?我尝试过试验并在Google上寻求帮助,但无济于事.

任何帮助都将非常感谢,谢谢.我的所有四个文件都在下面.

文件:Foo.h

typedef struct
{
    int number;
} my_struct;    // Redefinition; different basic types
Run Code Online (Sandbox Code Playgroud)

文件:Foo.c

#include "Foo.h"
#include "Bar.h"
#include <stdio.h>

my_struct test;

int main(void)
{
    test.number = 0;
    DoSomething(&test);
    printf("Number is: ", &test.number);
}
Run Code Online (Sandbox Code Playgroud)

文件:Bar.h

#include "Foo.h"

void DoSomething(my_struct *number);
Run Code Online (Sandbox Code Playgroud)

文件:Bar.c

#include "Bar.h"

void DoSomething(my_struct *number)
{
    number->number = 10;
}
Run Code Online (Sandbox Code Playgroud)

c struct typedef extern

10
推荐指数
1
解决办法
2万
查看次数

一种使用STL计算std :: string向量中的字符的方法?

因此,虽然找到std :: string向量中的字符数量是微不足道的,但我想知道是否有一种方法可以使用STL为您完成所有工作,而不是有两个for循环,一个到循环遍历向量,另一个循环遍历向量的每个索引中的字符串.

我尝试过使用其他STL函数(例如尝试以几种独特的方式使用std :: for_each),但我的所有尝试都没有成功.

int main(void)
{
  int chars = 0;
  std::vector<std::string> str;
  str.push_back("Vector");
  str.push_back("of");
  str.push_back("four");
  str.push_back("words");

  for(int i = 0; i < str.size(); ++i)
    for(int j = 0; j < str[i].size(); ++j)
      ++chars;

  std::cout << "Number of characters: " << chars; // 17 characters

  // Are there any STL methods that allows me to find 'chars'
  // without needing to write multiple for loops?

}
Run Code Online (Sandbox Code Playgroud)

c++ performance stl vector

4
推荐指数
2
解决办法
1329
查看次数

标签 统计

c ×1

c++ ×1

extern ×1

performance ×1

stl ×1

struct ×1

typedef ×1

vector ×1