- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我在这里找到了我正在使用的这段代码:
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
struct Something{
int x;
int y;
};
int main()
{
vector <Something> v;
int x, y;
cin >> x >> y;
Something temp;
temp.x = x;
temp.y = y;
v.push_back(temp);
for (size_t i = 0; i < v.size(); i++)
{
cout << v[i] << endl; // Error: No operator "<<" matches these operands. Operand types are std::ostream << Something
}
}
基本上,我试图将多个整数放入一个 vector 索引中。当我尝试打印 vector 的内容时,cout
不工作。
首先,我是否正确地执行了这个多重 int 操作?我对 C++ 比较陌生。
如果我这样做是对的,关于为什么 cout
不起作用的任何想法?我还尝试了 v.push_back({x,y})
但没有用。知道 cout
发生了什么吗?非常感谢。
编辑:到目前为止非常感谢。我还有一个问题。如果我要修改我的代码以接受多个输入,然后希望 vector 中的所有内容都根据“y”从大到小排序。
示例(原始 vector 内容(x,y))
12 4
1 2
4 10
1 1
1 2
根据'y'(从大到小)排序
4 10
12 4
1 2
1 2
1 1
我知道如何进行常规排序,但不知道如何根据第二个数字 (y) 进行排序。我怎么做?非常感谢。
最佳答案
正如错误所说,没有重载 operator<<
为您的结构声明的函数。
有三种可能的解决方案:第一种是输出结构体的每个成员,比如
std::cout << v[i].x << ' ' << v[i].y << '\n';
另一种是创建一个执行上述操作的函数,作为成员函数或非成员函数。
第三个解决方案是创建一个重载的 operator<<
结构函数:
std::ostream& operator<<(std::ostream& os, Something const& something)
{
return os << something.x << ' ' something.y;
}
我推荐你find a good beginners book on C++并阅读有关输出、结构和类以及运算符和运算符重载的章节。
关于c++ - 将多个整数添加到一个 vector 索引? 'Cout' 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37065214/
我想知道 cout<
这可能是一个初学者问题,了解 cout 的工作原理可能是这里的关键。如果有人可以链接到一个好的解释,那就太好了。 cout(&cout) 的地址. cout << &cout打印出 cout 的地址.
经过一整天的编码,我不小心写了 cout << "some text" << cout; 代替 cout << "some text" << endl; 现在它打印出一个内存地址。它指向什么? 最佳答
这与 difference-between-cout-x-and-cout-operator-x 有关问题,但还是有点不同... #include int main(){ std::cout
我是 C++ 的新手,最近我花了几天时间阅读有关指针的内容。我意识到下面的 2 段代码给我不同的结果,尽管它们看起来相同。 第一段代码: int a = 5; int* ptr = &a; cout
我尝试使用更短的语法并避免在任何地方使用 std::,所以我开始使用新的别名语法。在一些例子中,我看到人们这样使用它: using json = nlohmann::json; 并尝试使用 std::
这是我的头文件 #ifndef KINGDOM_H_ #define KINGDOM_H_ #include using namespace std; namespace sict{ cla
我经常遇到要将二维数组打印到屏幕或文件的情况。我的标准方法是这样的: for(int q=0; q #include void printNumber(int x) { std::cout
有一些 cout 语句,但第一个 cout 语句末尾的空格出现在第二个 cout 语句的开头。这是代码: #include int main() { using namespace std;
我在搞乱代码时遇到了这种相当模糊的行为,这是示例: #include using namespace std; int print(void); int main(void) { cout
我收到所有 cout 和 endl 的这些错误消息: main.cc:17:5: error: ‘cout’ was not declared in this scope main.cc:17:5:
这个问题在这里已经有了答案: What is the meaning of prepended double colon "::"? (9 个回答) 关闭 7 个月前。 有一个简单的代码,包含::操
我有下面的代码,我不太明白为什么结果恰好像下面这样: #include #include using namespace std; int main () { std::stringstre
在 C++ 中,当我在 .h 文件中声明自己的命名空间时,如下所示: namespace my_own { //... } 那么,如果我在命名空间 my_own 内部或外部声明 using s
背景 IIRC,来自 Release 2.0 C++ 将单字符常量存储为类型 char而不是 int .但是在 Release 2.0 之前声明如下 cout #include using name
Problem was in IDE I am using - CLion 1.2.4 gives incorrect output inside its own output window, sol
我知道有几个这样的拷贝,但到目前为止,没有一个对我有用。我正在尝试使用 g++ 在 Ubuntu 上编译一个非常简单的 C++ 程序,但它给了我范围错误。 #include using namesp
我在这里有一个难题,我无法解决,也没有在网上找到正确的答案: 我创建了一个带有清理路由的分离线程,问题是在我的 Imac 和 Ubuntu 9.1(双核)上。我无法正确取消空闲代码中的分离线程: #i
#include #include #include using namespace std; int main() { ofstream fout("test.txt"); f
我是一名优秀的程序员,十分优秀!