使用string :: pop_back()的编译错误

时间:2018-07-03 15:29:30

标签: c++ string c++11 substring stdstring

我正在尝试删除字符串的最后一个字符。由于我使用的是c ++ 11,因此我使用的是pop_back()

std::string ipAddresses = response.substr(response.find_first_of(",")+1);
responseVector.push_back( ipAddresses.pop_back() );

由于某种原因,我收到编译错误:

 error: invalid use of void expression
 responseVector.push_back( ipAddresses.pop_back() );

有人可以告诉我如何解决此问题吗?

2 个答案:

答案 0 :(得分:3)

您需要分两个步骤进行操作:

library(ineq)
x <-c(15000, 11000, 9000, 4000, 4000, 3900, 3800, 3600, 3400,
      1000, 900, 800, 700, 700, 400, 300, 300, 300, 200, 100)
n <- length(x)

best_sd <- 1
for(d in 2:n-2) for(u in 3:n-2){
  g <- c(Gini(x[1:d]), Gini(x[d+1:u]), Gini(x[u+1:n]))
  s <- sd(g) 
  if(s < best_sd){
    best_sd <- s
    best_grouping <- c(d,u)
    best_g <- g
  }
}

best_sd
#[1] 0.005250825
best_grouping
#[1]  9 11
best_g
#[1] 0.3046409 0.3144654 0.3127660

答案 1 :(得分:0)

您可以尝试一些简单的操作来删除 string 的最后一个字符:

const size_t length(ipAddresses.length());
if (length > 0)
{
  ipAddresses.erase((length - 1), 1);
}

上面的代码获取字符串的长度。
如果字符串中至少有一个字符,请删除最后一个字符。
注意,位置是从0开始的,因此减去1。

相关问题