C ++字符串附加问题

时间:2016-02-27 23:05:56

标签: c++ arrays string append

我现在遇到一些问题,尝试在设置c ++字符串的某些值后将char数组附加到c ++字符串上,我不明白为什么。我想知道你们中是否有人知道会发生什么。

这是我尝试运行的代码:

string test = "";
test.resize(1000);
char sample[10] = { "Hello!" };
test[0] = '1';
test[1] = '2';
test[2] = '3';
test[3] = '4';
test += sample;

通过调试器运行它,似乎test只是" 1234"和" Hello"永远不会添加。

提前致谢!

2 个答案:

答案 0 :(得分:3)

它被添加,但在字符串中已经有1000个字符之后(其中4个是1234,996是'\ 0'字符)`。

resize函数确实为字符串对象分配了1000个字符,但也将长度设置为1000.这就是为什么有时你要做的是使用string test = ""; test.reserve(1000); // length still 0, capacity: 1000 char sample[10] = { "Hello!" }; test.push_back('1'); // length is 1 test.push_back('2'); // length is 2 test.push_back('3'); // length is 3 test.push_back('4'); // length is 4 test += sample; // length is now 10

这通常是我要做的事情:

string test = "";
test.resize(1000); // length is 1000
char sample[10] = { "Hello!" };
test[0] = '1'; // length is 1000
test[1] = '2'; // length is 1000
test[2] = '3'; // length is 1000
test[3] = '4'; // length is 1000
test.resize(4); // length is now 4, but the internal buffer still has a capacity of 1000 characters
test += sample; // length is now 10

或者如果你想按自己的方式去做:

# Making adjustments to legend: adjusting position and title position
l2 <- ggplot(foo.long, aes(out121.perc,value,fill=variable))+
  geom_bar(position="stack",stat="identity")+
  scale_fill_manual(values=cbbPalette,name = "% of irrigation",
                    labels = c("0-10% irrigation", "10-20% irrigation", "20-30% irrigation", 
                               "30-40% irrigation", "40-50% irrigation", "50-60% irrigation",
                               "60-70% irrigation", "70-80% irrigation", "80-90% irrigation",
                               "90-100% irrigation"))+
scale_x_discrete(name="Threshold irrigation (in percentage)",breaks=c(0, 250, 500,750,1000),
                 labels=c("0", "25", "50","75","100")) +
  scale_y_continuous(name="number of farms",limits = c(0, 10000), breaks=c(0, 1000,2000,3000, 4000))+
  theme(legend.position="bottom", panel.background = element_rect(fill = NA),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank())+
  guides(fill=guide_legend(title="Number of irrigated farms",ncol=3, title.position = "top"))

l2

# ggplotGrob

g1 <- ggplotGrob(l2)
g2 <- ggplotGrob(l66)


# Add plots together
pp <- c(subset(g2$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g2, g1$grobs[[which(g1$layout$name == "panel")]], pp$t, 
                     pp$l, pp$b, pp$l)

# Add second axis for accuracy
ia <- which(g1$layout$name == "axis-l")
ga <- g1$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g1$widths[g1$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)


# Add second y-axis title 
ia <- which(g1$layout$name == "ylab")
ax <- g1$grobs[[ia]]
# str(ax) # you can change features (size, colour etc for these - 
# change rotation below 
ax$rot <- 270
g <- gtable_add_cols(g, g1$widths[g1$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)


# extract legend
leg1 <- g1$grobs[[which(g1$layout$name == "guide-box")]]
leg2 <- g2$grobs[[which(g2$layout$name == "guide-box")]]

legc = gtable:::cbind_gtable(leg1, leg2, "first")


g$grobs[[which(g$layout$name == "guide-box")]] <- 
  gtable:::cbind_gtable(leg1, leg2, "first")
grid.draw(g)  # Note: Legend does not fit

g$heights[[6]] = unit(5, "cm")   # Add more space for the legend - can adjust this to suit
grid.draw(g)

答案 1 :(得分:0)

我认为问题是当你test.resize(1000)向字符串添加了1000 空字符'\0')时。调试器可能会将空字符视为字符串结尾标记。因此,在这些空字符后添加的任何文本都不会显示。

说文本等于此('_' = 空字符行结束标记):

test = "1234_______________Hello!"; 
            ^
            Debugger thinks text ends here