如何在C ++中将值添加到未知大小的数组中?

时间:2018-03-06 22:58:46

标签: c++ append

就像我在Python中一样,

Private Function GetColumnTypeDefinition(ByVal TableSchemaRow As DataRow) As String
    Dim ColumnTypeDefinition As String = "character varying"

    Dim CurrentType As String = Convert.ToString(TableSchemaRow("DataType"))
    Dim ColumnSize As Integer = Convert.ToInt32(TableSchemaRow("ColumnSize"))
    Dim AutoIncrement As Boolean = Convert.ToBoolean(TableSchemaRow("IsAutoIncrement"))
    Dim AllowNull As Boolean = Convert.ToBoolean(TableSchemaRow("AllowDBNull"))
    Dim NumericScale As Integer = 0
    Dim NumericPrecision As Integer = 0
    Dim DefaultValue As String = String.Empty

    If Not IsCellEmpty(TableSchemaRow("DefaultValue")) Then
        DefaultValue = Convert.ToString(TableSchemaRow("DefaultValue"))
    End If

    ' TOTAL NUMBER OF DIGITS IN A NUMBER (BEFORE AND AFTER A DECIMAL POINT)
    If Not IsCellEmpty(TableSchemaRow("NumericPrecision")) Then
        NumericPrecision = Convert.ToInt32(TableSchemaRow("NumericPrecision"))
    End If

    ' NUMBER OF DIGITS AFTER THE DECIMAL POINT IN A NUMBER
    If Not IsCellEmpty(TableSchemaRow("NumericScale")) Then
        NumericScale = Convert.ToInt32(TableSchemaRow("NumericScale"))
    End If

    Select Case CurrentType.ToString
        Case "System.Boolean"
            ColumnTypeDefinition = "boolean"
        Case "System.String"
            If ColumnSize = -1 Then
                ColumnTypeDefinition = "character varying"
            Else
                ColumnTypeDefinition = "character varying(" & ColumnSize & ")"
            End If
        Case "System.Decimal"
            If NumericScale > 0 Then
                If NumericPrecision > 0 Then
                    ColumnTypeDefinition = "numeric(" & NumericPrecision & "," & NumericScale & ")"
                Else
                    ColumnTypeDefinition = "numeric(" & NumericScale + 1 & "," & NumericScale & ")"
                End If
            ElseIf NumericPrecision > 10 Then
                ColumnTypeDefinition = "bigint"
            Else
                ' >> THIS IS WHERE I WOULD WANT TO QUERY THE ORIGINAL 
                ' DATATABLE INSTEAD OF JUST AUTOMATICALLY DEFAULTING 
                ' TO AN INTEGER DATATYPE <<
                ColumnTypeDefinition = "integer"
            End If
        Case "System.Double"
            ColumnTypeDefinition = "double precision"
        Case "System.Single"
            ColumnTypeDefinition = "real"
        Case "System.Int64", "System.Long"
            If AutoIncrement Then
                ColumnTypeDefinition = "bigserial"
            Else
                ColumnTypeDefinition = "bigint"
            End If
        Case "System.Int32", "System.Integer"
            If AutoIncrement Then
                ColumnTypeDefinition = "serial"
            Else
                ColumnTypeDefinition = "integer"
            End If
        Case "System.Int16", "System.Short"
            If AutoIncrement Then
                ColumnTypeDefinition = "smallserial"
            Else
                ColumnTypeDefinition = "smallint"
            End If
        Case "System.DateTime"

        Case Else
            Throw New NotImplementedException(CurrentType.ToString & " not implemented.")
    End Select

    Return ColumnTypeDefinition
End Function

我知道如何使用for循环在C ++中附加数据。但是如何初始化未知大小的数组呢?它的大小取决于循环部分中附加的数据点的数量。

1 个答案:

答案 0 :(得分:1)

您的代码实际上使用的是Python list,而不是Python array

在C ++中,与您的代码最匹配的是使用std::vector

// In a header:
#include <string>
#include <vector>

// In your code
auto arr = std::vector<std::string>();
std::string data = "Hello world!";
arr.push_back( data );