为什么不使用不关心长度的容器 - 例如, std::string
?
现在,我正在最近使用通用csv格式提供的TZ db(例如in a file from CERN),但在Boost源中也使用了相同的格式。
有了这些数据,我看到的28:最大长度
R> library(RcppBDT) # R package interfacing Boost Date_Time
Loading required package: Rcpp
R> tz <- new(bdtTz, "America/Chicago") # init. an object, using my default TZ
R> tznames <- tz$getAllRegions() # retrieve list of all TZ names
R>
R> length(tznames) # total number of TZ identifiers
[1] 381
R>
R> head(tznames) # look at first six
[1] "Africa/Abidjan" "Africa/Accra" "Africa/Addis_Ababa"
[4] "Africa/Algiers" "Africa/Asmera" "Africa/Bamako"
R>
R> summary(sapply(tznames, nchar)) # numerical summary of length of each
Min. 1st Qu. Median Mean 3rd Qu. Max.
9 13 15 15 17 28
R>
R> tznames[ nchar(tznames) >= 26 ] # looking at length 26 and above
[1] "America/Indiana/Indianapolis" "America/Kentucky/Louisville"
[3] "America/Kentucky/Monticello" "America/North_Dakota/Center"
R>
我们也可以看看直方图:
R> library(MASS)
R> truehist(sapply(tznames, nchar),
+ main="Distribution of TZ identifier length", col="darkgrey")
R>
![enter image description here](https://i.stack.imgur.com/NNO4C.png)
这使用,我有代码在我的RcppBDT包的SVN repo on R-Forge中,但还没有在包的CRAN version中。
令人惊叹的答案!实际上,我不能使用可变长度的字符串,因为我想将这些字符串存储在数据库中。起初,我不想使用任意长的char字段,但我认为我设置了一个长度为40个字符的字符串[1]。我将把它们放在一个单独的表格中,并使用外键引用它们。 1:有一个实际上是32个字符的时区(“America/Argentina/ComodRivadavia”)。将来可能会有其他同样长的名字。 – sleblanc
你不能在SQL中使用varchar吗? –
varchars实际上是否需要指定的长度? – sleblanc