在C++中打印多个矢量的第一个值
Printing the first value from more than one vector in C++
我正试图从下面主函数中显示的每个向量中打印第一个值。
#include <iomanip>
#include <map>
using namespace std;
typedef unsigned int vect;
int main() {
std::vector<vect> p;
vector<vect> a = { 4,2,3,1 };
vector<vect> b = { 4,2,3,1 };
vector<vect> c = { 4,2,3,1 };
vector<vect> d = { 4,2,3,1 };
int i;
for (i=0; i<a.size(); i++)
cout << a[i];
}
函数first_preference((来自下方显示的函数.cpp
#include "function.h"
#include <string>
#include <iostream>
using namespace std;
person test::first_preference() const {
const person& first = p.front();
return first; //current first pref
}
该函数在这个头类中声明
#ifndef FUNCTION_H
#define FUCNTION_H
#include <vector>
typedef unsigned int person;
typedef unsigned int vect;
std::vector<vect> p;
class test {
public:
person first_preference() const;
};
#endif
我想从函数应该打印每个向量的第一个值的main((调用函数first_preference((,我该如何处理?
我希望从
main()
调用函数first_preference()
,函数应该打印每个向量的第一个值
一些问题:
-
您的头文件中有一个全局
std::vector<vect> p
(这不是一个好主意(,它被main
中的std::vector<vect> p
遮蔽。在main
中放入p
的内容将无法从test
的实例访问。这些实例只知道全局CCD_ 9。 -
main.cpp
中没有#include "function.h"
,因此无法在main
中创建test
对象。 -
如果您在
main.cpp
中#include "function.h"
,则不需要typedef unsigned int vect;
,因为您已经在function.h
中这样做了。这不是一个错误,而是令人困惑和不必要的。 -
vector<vect>
实例a, b, c
和d
与test
或任何p
都没有任何连接,因此除非以某种方式将它们传递给test
,否则您在这些向量中输入的内容不可能由test
的实例打印。 -
您声明了
vect
的向量,但first_preference()
按值返回person
。vect
和person
恰好是相同基本类型的别名,但这个接口似乎有问题。 -
在
main.cpp
中,您不实例化test
,而是对a
进行迭代,并且从未调用过first_preference()
,因此没有希望使用它。 -
为什么"使用命名空间std;"被认为是不好的做法?
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- 如何仅读取文本文件中的第一个值
- 在C++中,如何在第一个"system()"结束后执行第二个"system()"?
- 查找不在标准中的第一个值::设置<int>最小-最大值
- C++:忽略第一个 cin.ignore 之后的输入
- 在C++中打印多个矢量的第一个值
- C++去除前x个元素的有效方法,在不改变向量大小的情况下将第x+1个元素推到第一个
- C++第一个cout将不会打印
- 我们可以在第一个else-if条件结束后使用另一个else-if条件吗
- OpenGL:第二个VBO破坏了第一个VBO
- 为什么第一个Dynamic_cast没有投射到基类?
- OpenGL 2D游戏只绘制第二个精灵纹理而不是第一个
- C++ 为什么程序只读取第一个值
- 在我的第一个C++程序中需要一些帮助(简单)
- 为什么我的代码在第一个 if 语句处中断?
- 是否可以从另一个类对象调用一个类函数而不继承第一个类
- 无法使我的第一个Windows OpenGL窗口抬起并运行
- 将参数初始化为构造函数,而不是第一个
- 无法在硬件模式下创建 SGX 安全区 - "invalid launch token"即使文档将无效的启动令牌指定为第一个