C++中字符或者数字格式控制符setprecision()、fixed内容在<iomapip>类中给定,<iso>类中manipulators中格式输出格式相关的控制有scientific等格式控制。
1. iomapip和ios类组合
**如果scientific和setprecision(n)合用,可以控制指数表示法的小数位数。**scientific是设置指数方式的小数表示法。
#include<iostream>
#include<iomanip> //要用到格式控制符
using namespace std;
int main(){
double amount = 22.0/7;
cout<<amount<<endl;
cout<<setprecision(0)<<amount<<endl
<<setprecision(1)<<amount<<endl
<<setprecision(2)<<amount<<endl
<<setprecision(3)<<amount<<endl
<<setprecision(4)<<amount<<endl;
cout<<fixed<<setprecision(8)<<amount<<endl;
cout<<scientific<<amount<<endl;
cout<<setprecision(6); //重新设置成原默认设置
}
/*运行结果:
3.14286
3
3
3.1
3.14
3.143
3.14285714
3.14285714e+000
*/
分析:
第1行输出数值之前没有设置有效位数,所以用流的有效位数默认设置值6。第2行输出设置了有效位数0,C++最小的有效位数为1,所以作为有效位数设置为1来看待。第 3~6 行输出按设置的有效位数输出。
第7行输出是与 fixed合用,所以setprecision(8)设置的是小数点后面的位数,而非全部数字个数。
第 8行输出用 scientific 来表示指数表示的输出形式。其有效位数沿用上次的设置值8。
总结:
1、在普通表示的输出中,setprecision(n)表示有效位数。
2、在确定表示的输出中,即和fixed连用的时候,setprecision(n)表示小数位数。
3、在指数形式输出时,即和scientific连用的时候,setprecision(n)表示小数位数。
4、小数位数截断显示时,进行四舍五入处理。
5、注意操作符的效力。
#include <iostream>
#include <iomanip>
void r_coutHex(const unsigned char * p,int len)
{
int i = 0;
for (; i<len; i++)
{
std::cout<<std::hex<<std::setw(2)<<std::setfill('0')<<(unsigned int)(p[i])<<" ";
}
std::cout<<std::endl;
}
运行结果如下图:第2个框,输入自传入哈(博客传图失败,汗)
5a a5 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 20 20 ff a3 5e
2. 颜色控制
今天我们谈一下R中的颜色系统。
一般R中的作图函数,也就是说,即使我们不特定指定颜色,一般的作图函数,也会有自己默认的颜色。但是很多时候,这些颜色并不是很美观,所以适当地改变配色,对我们的工作有一定的意义。然后我们下面介绍一下R中的颜色系统。
注:
1、R中有4种颜色表达方式
palette中的颜色,直接使用数字指定相应的颜色
颜色名,如“green”“blue”“red”等等颜色的名称
使用rgb函数得到的返回颜色
使用十六进制颜色代码,如"#FF0000"等等
2、R很难直接查看某种颜色(直接根据上面的四种表达方式),所以一个好的办法是使用plot,指定颜色col,得到一张带颜色的图
> palette()
[1] "black" "red" "green3" "blue" "cyan" "magenta" "yellow" "gray"
> barplot(1:4, col = 1:4)
一、grDevices
grDevices是R默认安装的包,使用的时候不用加载即可以使用,这个函数有几个颜色的函数。
1、 首先是palette()这个函数
可以理解为R自带的一个简单调色板,可以通过指定颜色为数字的方式,直接使用颜色
#查看当前palette中的颜色
palette()
[1] "black" "red" "green3" "blue" "cyan" "magenta" "yellow" "gray"
#用barplot查看颜色
barplot(1:8, col = 1:8)
Barplot(palette默认颜色)
#注意:
#palette里面颜色是可以改变的,一般不指定是默认的颜色,即上面显示的8种颜色
#如果演变了palette里面的颜色,可以通过设置palette的参数为default的方式将颜色改变成默认颜色
> palette(gray(c(0.1,0.2, 0.3)))
> palette()
[1] "gray10" "gray20" "gray30"
#改回default
> palette("default")
> palette()
[1] "black" "red" "green3" "blue" "cyan" "magenta" "yellow" "gray"
2、colors函数
colors()函数会返回657种R种可使用的颜色的名字,具体想看那种颜色的话可以通过barplot或者pie图的方式查看
#colors()共有657种颜色
> head(colors())
[1] "white" "aliceblue" "antiquewhite" "antiquewhite1" "antiquewhite2"
[6] "antiquewhite3"
3、colorRamp和colorRampPalette
colorRampPalette比较容易理解,就是颜色的渐变色混合。
colorRamp则有些不同,因为颜色是使用rgb来表示的,输入参数为0到1之间的值,输出的颜色会根据输入值进行组合,得到的rgb颜色会和colorRampPalette有所不同。
共同点是上面两个函数输入值以后,都返回的是一个函数,需要进一步指定函数的输入值,才能得到颜色。
可参考下面的解释:
colorRamp 和 colorRampPalette 函数
这两个函数会接受一个色彩向量作为输入, 指定颜色变换的节点, 其返回值均是一个函数, 该返回的函数可以根据需要返回计算出的位于指定的颜色节点之间的过渡颜色. 区别在于, colorRamp 的返回函数接受的输入值为 0 到 1 的数字, 返回相对应的 RGB 颜色, 而 colorRampPalette 则接受一个整数, 返回相应数量的颜色作为调色板.
R 学习笔记:R 色彩
二、RColorBrewer
RColorBrewer包是一个有用的配色包,我们可以直接使用上面的颜色作图,需要先安装这个package。
里面的颜色适合三种情况:
sequential,连续型,适合连续型的数值变量
diverging,极端型,适合数据存在极端值,中间变化不大,两边变化较大的情况
qualitative,离散型,变化互不相关,如因子变量等
RColorBrewer比较简单,里面基本上只有几个可用函数,如下
brewer.pal makes the color palettes from ColorBrewer available as R palettes. 使用brewer.pal选取颜色
display.brewer.pal() displays the selected palette in a graphics window.
display.brewer.all() displays the a few palettes simultanueously in a graphics window.
brewer.pal.info returns information about the available palettes as a dataframe.brewer.pal.info is not a function, it is a variable. This might change in the future.
#使用brewer.pal.info查看颜色信息
> head(brewer.pal.info)
maxcolors category colorblind
BrBG 11 div TRUE
PiYG 11 div TRUE
PRGn 11 div TRUE
PuOr 11 div TRUE
RdBu 11 div TRUE
RdGy 11 div FALSE
#使用display.brewer.all()查看所有可用颜色
> display.brewer.all()
#举例说明
pal <- colorRamp(c("red", "blue"))
pal(0)
pal(1)
barplot(1:10, 1:10, col = pal(seq(0, 1, len = 10)))
#####################
pal <- colorRampPalette(c("red", "yellow"))
pal(1)
pal(2)
barplot(1:10, 1:10, col = pal(10))
#######################
library(RColorBrewer)
brewer.pal.info
display.brewer.all()
display.brewer.pal(10, "Spectral")
display.brewer.pal(13, "RdGy")
barplot(1:10, 1:10, col = brewer.pal(10, "YlOrRd"))
barplot(1:9, 1:9, col = brewer.pal(9, "Blues"))
barplot(1:9, 1:9, col = brewer.pal(9, "Spectral"))
barplot(1:9, 1:9, col = brewer.pal(9, "Set1"))
########################################
cols <- brewer.pal(3, "Spectral")
cols
pal <- colorRampPalette(cols)
image(volcano, col = pal(10))
image(volcano, col = pal(20))
image(volcano, col = pal(50))
image(volcano, col = pal(100))
image(volcano, col = pal(20000))
最后提一下rgb函数,其中alpha为透明度,0为完全透明,1为完全不透明
> str(rgb)
function (red, green, blue, alpha, names = NULL, maxColorValue = 1)
注,我们上面讲的基本上涉及到了RGB的表达方式,计算机中的颜色表达可以有多重方式,如RGB、HSV、HSL等,我个人认为了解即可,如需进一步了解,可参考
R 学习笔记:R 色彩
#include <iostream>
#include <string>
using namespace std;
int main(){
cout << "这是原始颜色" << endl;
cout << "\033[30;1m这是黑色\033[0m" << endl;
cout << "\033[31;1m这是红色\033[0m" << endl;
cout << "\033[32;1m这是绿色\033[0m" << endl;
cout << "\033[33;1m这是黄色\033[0m" << endl;
cout << "\033[34;1m这是蓝色\033[0m" << endl;
cout << "\033[35;1m这是紫色\033[0m" << endl;
cout << "\033[36;1m这是青色\033[0m" << endl;
cout << "\033[37;1m这是白色\033[0m" << endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
void red_print(string out){
cout << "\033[31;1m" << out << "\033[0m" << endl;
}
int main(){
red_print("红色输出...");
return 0;
}
2.3 输出带颜色log日志
#ifndef _LIGHT_LOG_H
#define _LIGHT_LOG_H
#include <iostream>
#include <ostream>
#include <fstream>
#include <sstream>
#include <string>
#include <memory>
#include <mutex>
#include <cstdlib>
namespace llog {
enum LOG_LEVEL {
LOG_LEVEL_INFO,
LOG_LEVEL_WARNING,
LOG_LEVEL_ERROR
};
// 定义不同类型对应的颜色类型
#define COLOR_ERRO 31 //31
#define COLOR_INFO 37 //37
#define COLOR_WARN 33 //33
#define COLOR_FILE 35 //35
#define COLOR_LINE 32 //32
#define COLOR_FUNC 36 //36
#define DELIMITER " "
class Message {
private:
std::string head; // 消息头
std::string head_color; // 带颜色的消息头
std::string msg; // 消息
std::string colorStr(int color, int bold, const std::string & delimiter, const std::string & t, bool if_color = true) {
std::stringstream ss;
if (if_color)
ss << "\033[" << bold << ";" << color << "m";
ss << t;
if (if_color)
ss << "\033[0m";
ss << delimiter;
return ss.str();
}
// 默认开启颜色输出
std::string logHeader(int level, std::string file, std::string function, int linenumber, bool if_color = true) {
std::stringstream ss;
std::stringstream st;
int color = 39;
if (level==LOG_LEVEL_ERROR) { color = COLOR_ERRO; st << "[ERRO]"; }
if (level==LOG_LEVEL_INFO) { color = COLOR_INFO; st << "[INFO]"; }
if (level==LOG_LEVEL_WARNING) { color = COLOR_WARN; st << "[WARN]"; }
ss << colorStr(color, 1, DELIMITER, st.str(), if_color);
// 格式化时间戳
time_t rawtime;
struct tm* timeinfo;
char timeBuffer[30];
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(timeBuffer, 30, "%Y/%m/%d-%H:%M:%S", timeinfo);
ss << colorStr(color, 0, DELIMITER, timeBuffer, if_color);
int pPos = 0;
for (int i = file.length()-1; i > 0; i--)
if (file.substr(i,1)==std::string("/"))
{ pPos = i+1; break; }
file = file.substr(pPos,file.length()-pPos);
ss << colorStr(COLOR_FILE, 0, ":", file, if_color); //35
ss << colorStr(COLOR_LINE, 1, ":", std::to_string(linenumber), if_color); //32
ss << colorStr(COLOR_FUNC, 0, DELIMITER, function, if_color); //36
return ss.str();
}
public:
Message(const int level = 0, std::string file = nullptr, std::string function = nullptr, const int linenumber = 0) {
head_color = logHeader(level, file, function, linenumber);
head = logHeader(level, file, function, linenumber, false);
}
template <typename T>
Message& operator<<(const T & t) {
std::stringstream ss;
ss << t;
msg += ss.str();
return *this;
}
std::string get_string(bool if_color = true) const {
if (if_color)
return head_color + msg;
return head + msg;
}
};
// Singleton Class
class Logger {
private:
static std::unique_ptr<Logger> m_instance_;
LOG_LEVEL m_level_;
static std::mutex mtx_;
bool console_;
bool file_;
std::ofstream log_file;
Logger() {
if (getenv("LLOG_LV") == NULL)
m_level_ = LOG_LEVEL_INFO;
else{
std::string ev_level = getenv("LLOG_LV");
if (ev_level == "WARN")
m_level_ = LOG_LEVEL_WARNING;
else if (ev_level == "ERRO")
m_level_ = LOG_LEVEL_ERROR;
else
m_level_ = LOG_LEVEL_INFO;
}
console_ = true;
file_ = false;
};
public:
// 单例模式
static Logger* get_instance() {
if (m_instance_ == nullptr) {
mtx_.lock();
if (m_instance_ == nullptr) {
m_instance_.reset(new Logger);
}
mtx_.unlock();
}
return m_instance_.get();
}
void init_log_file(std::string log_file_name = "output.log") {
if (file_ == true)
log_file.close();
log_file.open(log_file_name);
file_ = true;
}
void set_level(int level) {
m_level_ = LOG_LEVEL(level);
}
int get_level() const {
return m_level_;
}
void set_console(bool b) {
console_ = b;
}
int get_console() const {
return console_;
}
void operator+=(const Message& m)
{
mtx_.lock();
if (console_)
std::cout << m.get_string() + '\n';
if (file_)
log_file << m.get_string(false) + '\n';
mtx_.unlock();
}
};
std::unique_ptr<Logger> Logger::m_instance_ = nullptr;
std::mutex Logger::mtx_;
# define LLOG(level) if (level >= (*llog::Logger::get_instance()).get_level()) (*llog::Logger::get_instance()) += llog::Message(level, __FILE__, __FUNCTION__, __LINE__)
# define INFO 0
# define WARN 1
# define ERRO 2
# define LLOG_INFO LLOG(INFO)
# define LLOG_WARN LLOG(WARN)
# define LLOG_ERRO LLOG(ERRO)
# define LLOG_LV(level) (*llog::Logger::get_instance()).set_level(level)
# define LLOG_LV_INFO LLOG_LV(INFO)
# define LLOG_LV_WARN LLOG_LV(WARN)
# define LLOG_LV_ERRO LLOG_LV(ERRO)
# define LLOG_FILE(fn) (*llog::Logger::get_instance()).init_log_file(fn);
# define LLOG_CONSOLE_OFF() (*llog::Logger::get_instance()).set_console(false)
# define LLOG_CONSOLE_ON() (*llog::Logger::get_instance()).set_console(true)
} //namespace llog
#endif // _LIGHT_LOG_H