cout

20190814-华为笔试--找到用户i的所有n度好友 C++

倖福魔咒の 提交于 2019-11-27 10:24:11
1.题目描述:   某软件用户共m个人,编号为0~m-1,用r[i][j]表示用户i和j的好友关系,r[i][j]=0说明不是好友,r[i][j]=1~9数值越大关系越好。其次,r[i][j] = r[j][i]。找出i的n度好友。若不存在n度好友则输出-1.   1度好友代表直接好友,2度好友代表好友的好友,,。按照推荐值降序输出好友列表。即r[i][j] = 6,r[j][k]=4,则i的2度好友推荐值为r[i][j]+r[j][k]=10.   输入:第一行T表示T组测试数据;      第二行首先是整数k,接着3*k个整数,用空格隔开,形成<x,y,r[x][y]>的关系对。     示例:2        10 5 2        13 0 3 5 0 4 9 0 6 8 0 7 5 1 2 6 1 6 3 2 9 7 3 4 3 3 5 3 3 8 3 3 9 3 5 8 9 7 8 9         10 0 2        13 0 3 5 0 4 9 0 6 8 0 7 5 1 2 6 1 6 3 2 9 7 3 4 3 3 5 3 3 8 3 3 9 3 5 8 9 7 8 9   输出:输出T行,每行对应每组测试数据 i的n度好友的降序输出。     示例:7 0 4 9        1 5 8 9 2.理解:   使用dp[i][j

C++命名空间

青春壹個敷衍的年華 提交于 2019-11-27 10:22:22
C++引入命名空间,作为附加信息来区分不同库中相同名称的函数,类,变量等,使用了命名空间即定义了上下问,本质上命名空间就是定义了一个范围。 定义命名空间: 命令空间的定义使用关键字namespace,后面跟命名空间的名称,如下所示: namespace namespace_name { //代码声明 } 为了调用带有命名空间的函数或变量,需要在前面加上命名空间的名称,如下: name::code; 采用实例来看看命名空间如何为变量或函数等实体定义范围 /*** namespace.cpp ***/ #include<iostream> using namespace std; namespace first_space { void func() { cout << "Inside first_space " << endl; } } namespace second_space { void func() { cout << "Inside second_space " << endl; } } int main() { first_space::func(); second_space::func(); return 0; } 运行结果: exbot@ubuntu:~/wangqinghe/C++/20190814$ g++ namespace.cpp -o namespace

How to cout a float number with n decimal places [duplicate]

情到浓时终转凉″ 提交于 2019-11-27 09:01:32
Possible Duplicate: How do I print a double value with full precision using cout? float a = 175.; cout << a; If I run the previous code I'll get just 175, how can I cout the number with (for example) 3 decimal places even they were zeros .. How can I print "175.000" ?! Jesse Good You need std::fixed and std::setprecision : std::cout << std::fixed << std::setprecision(3) << a; These require following header: #include <iomanip> Try setprecision : cout.setf(ios::fixed); cout << setprecision(3) << a << endl; 来源: https://stackoverflow.com/questions/14677448/how-to-cout-a-float-number-with-n-decimal

cout a string gets the address instead of value [duplicate]

自作多情 提交于 2019-11-27 08:52:16
问题 This question already has answers here : How I can print the wchar_t values to console? (7 answers) Closed 5 years ago . There is a macro defined as below: #ifdef UNICODE typedef wchar_t TCHAR; #define TEXT(quote) L##quote #else typedef char TCHAR; #define TEXT(quote) quote #endif When I try to print a message using std::cout as below: TCHAR* test = TEXT("test"); cout << test; What I get the address such as 00D82110 instead of the value "test". Can anybody give any suggestion how can I print

How can I print a string to the console at specific coordinates in C++?

我们两清 提交于 2019-11-27 08:46:49
I'm trying to print characters in the console at specified coordinates. Up to now I have been using the very ugly printf("\033[%d;%dH%s\n", 2, 2, "str"); But I just had to ask whether C++ had any other way of doing this. The problem is not even that it's ugly, the problem comes up when I try to make myself a prettier function like so: void printToCoordinates(int x, int y, string text) { printf("\033[%d;%dH%s\n", x, x, text); } It doesn't work, even if I typecast to (char*) . Another problem is that I have to print out the \n for the page to be refreshed... I just don't enjoy using printf in

C++ cout cin string manipulation

有些话、适合烂在心里 提交于 2019-11-27 08:33:16
问题 I'm trying to get a line as input from the command line. My problem is that I'm not getting the whole line, but it's being tokenized by space. So if I entered something such as "I like Math a lot" instead of getting "you enterend: I like Math a lot" I get the follwoing: EDITING MODE: Enter a command i like Math a lot you entered i EDITING MODE: Enter a command you entered like EDITING MODE: Enter a command you entered Math EDITING MODE: Enter a command you entered a EDITING MODE: Enter a

文件和流

佐手、 提交于 2019-11-27 08:15:40
iostream标准库提供了cin和cout方法用于标准输入读取流和向标准输出写入流。 从文件读取流和向文件写入流,需要用到fstream库。它定了三个数据类型 数据类型 描述 ofstream 该数据类型表示输出文件流,用于创建文件并向文件写入信息 ifstream 该数据类型表示输入文件流,用于从文件读取信息 fstream 该数据类型通常表示文件流,同时具有ofstream和ifstream两种功能,这意味着它可以创建文件,从文件中读取信息,向文件写入信息 在C++中进行文件处理,必须在C++源代码文件中包含头文件<iostream>和<fstream> 打开文件 在从文件读取信息或向文件写入信息之前,必须先打开文件。ofstream和fstream对象都可以用来打开文件进行写操作,如果只需要打开文件进行对文件,则使用ifstream对象。 下面是open()函数的标准语法,open()函数是fstream、ifstream、ofstream对象的一个成员。 void open(const char *filename,ios::openmode mode); 在这里open()成员函数的第一个参数指定要打开的文件的名称和位置,第二个参数定义文件被打开的模式。 模式标志 描述 ios::app 追加模式,文件写入都追加到文件末尾 ios::ate 文件打开后定位到文件末尾

cin,cout,加速

坚强是说给别人听的谎言 提交于 2019-11-27 07:42:07
//解除的是C++运行库层面的对数据传输的绑定,取消输入输出时数据加入缓存,以提高效率 ios : : sync_with_stdio ( 0 ) ; cin . tie ( 0 ) ; cout . tie ( 0 ) ; 来源: https://blog.csdn.net/weixin_43820352/article/details/99555805

《PCL》点云的最小外包围盒实现

和自甴很熟 提交于 2019-11-27 07:40:47
#include <iostream> #include <pcl/io/pcd_io.h> #include <pcl/point_types.h> #include <pcl/common/common.h> int main(int argc, char** argv) { //定义一个点云cloud //pcl::PointCloud<pcl::PointXYZ> cloud; pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); if (pcl::io::loadPCDFile<pcl::PointXYZ>("E:\\abx.pcd", *cloud) == -1) { std::cout << "读取失败"; } //定义储存极值的两个点 pcl::PointXYZ minPoint, maxPoint; pcl::getMinMax3D(*cloud,minPoint, maxPoint); //输出结果 std::cout << "max:x" << maxPoint.x << std::endl; std::cout << "max:y" << maxPoint.y << std::endl; std::cout << "max:z" << maxPoint.z

异常处理

主宰稳场 提交于 2019-11-27 07:14:19
异常时程序在执行期间产生的问题。C++异常是指在程序运行时发生的特殊情况,比如尝试除以零的操作。 异常提供了一种转移程序控制权的方式。C++异常处理涉及到三个关键字: try、catch、throw l throw: 当问题出现时,程序回抛出一个异常。这是通过使用throw关键字来完成。 l catch: 当你想处理问题的地方,通过异常处理程序捕获异常。catch关键字用于异常捕捉。 l try: try块中的代码标识将被激活的特定异常。它后面通常跟着一个或多个catch块。 如果有一个块抛出一个异常,捕获异常的方法会使用try和catch关键字。try中放置可能抛出异常的代码。try块中的代码被称为保护代码。使用try/catch语句的语法如下: try { //保护代码 } catch(ExceptionName e1) { //catch块 } catch(ExceptionName e2) { //catch块 } catch(ExceptionName eN) { //catch块 } 如果try块在不同情境下会抛出不同的异常,这个时候就可以尝试罗列出多个catch语句,用于捕获不同的异常。 抛出异常 可以使用throw语句在代码块中的任何地方抛出异常,throw语句的操作数可以时任意的表达式,表达式的结果的类型决定了抛出的异常的类型。 实例: /*** try.cpp