将一个向量附加到另一个向量

时间:2018-01-12 22:29:21

标签: c++

我想动态地将一个向量附加到另一个向量,以便我可以构建一个矩阵x

int main()
{
    vector< vector<float> > x;
    vector<float> y = {1, 2, 3};
    x.insert(x.end(), y.begin(), y.end()) ;

    return 0;
}

但这给了我一个错误:

/usr/include/c++/5/bits/stl_algobase.h:340: error: no match for 'operator=' (operand types are 'std::vector<float>' and 'float')
        *__result = *__first;
                  ^

有什么想法吗?

4 个答案:

答案 0 :(得分:4)

  

有什么想法吗?

您正尝试将float添加到vector<vector<float>>。这就是问题所在。

如果您将x更改为

vector<float> x;

另一条线将起作用。

如果按原样保留x,则可以y添加x作为x.push_back(y); 的元素:

public class Main {


    public static void main(String[] args){
        String password = "1Aallll";
        String l3password = "1A ";
        String l2password = "aAaaaaa";
        String l1password = "1Aallll";

        System.out.println("isLongEnough " + isLongEnough(password));
        System.out.println("hasNoSpaces " + hasNoSpaces(password));
        System.out.println("atLeastOneUpper " + atLeastOneUpper(password));
        System.out.println("atLeastOneLower " + atLeastOneLower(password));
        System.out.println("atLeastOneDigit " + atLeastOneDigit(password));

        System.out.println("isLevel1 " + isLevel1(l3password));
        System.out.println("isLevel1 " + isLevel1(l2password));
        System.out.println("isLevel1 " + isLevel1(l1password));

        System.out.println("isLevel2 " + isLevel2(l3password));
        System.out.println("isLevel2 " + isLevel2(l2password));
        System.out.println("isLevel2 " + isLevel2(l1password));
    }

    public static boolean isLevel1(String pwd) {
        int howMany = 0;
        if (isLongEnough(pwd) && hasNoSpaces(pwd)) {
            if (atLeastOneUpper(pwd)) {
                howMany++;
            }
            if (atLeastOneLower(pwd)) {
                howMany++;
            }
            if (atLeastOneDigit(pwd)) {
                howMany++;
            }
            if (howMany >= 2) {
                return true;
            }
        }
        return false;

    }

    public static boolean isLevel2(String pwd) {
        if (isLongEnough(pwd) && hasNoSpaces(pwd) && atLeastOneUpper(pwd) && atLeastOneLower(pwd) && atLeastOneDigit(pwd)) {
            return true;
        }
        return false;
    }

    private static boolean isLongEnough(String pwd) {
        if (pwd.length() >= 6) {
            return true;
        }
        return false;
    }

    private static boolean hasNoSpaces(String pwd) {
        boolean flag = false;
        for (int i = 0; i < pwd.length(); i++) {
            if (!pwd.contains(" ")) {
                flag = true;
            }
        }
        return flag;
    }

    private static boolean atLeastOneUpper(String pwd) {
        boolean flag = false;
        for (int i = 0; i < pwd.length(); i++) {
            if (Character.isUpperCase(pwd.charAt(i))) {
                flag = true;
            }
        }
        return flag;
    }

    private static boolean atLeastOneLower(String pwd) {
        boolean flag = false;
        for (int i = 0; i < pwd.length(); i++) {
            if (Character.isLowerCase(pwd.charAt(i))) {
                flag = true;
            }
        }
        return flag;
    }

    private static boolean atLeastOneDigit(String pwd) {
        boolean flag = false;
        for (int i = 0; i < pwd.length(); i++) {
            if (Character.isDigit(pwd.charAt(i))) {
                flag = true;
            }
        }
        return flag;

    }
}

答案 1 :(得分:1)

vector<vector<float>> x;
vector<float> y = {1, 2, 3};
x.push_back(y);

x.insert(x.end(), y.begin(), y.end());尝试将float插入vector<vector<float>>

答案 2 :(得分:1)

你可以这样做:

x.push_back(y);

答案 3 :(得分:0)

使用x.insert(x.end(), y.begin(), y.end()),您将float值插入vector<float> - 期望对象。

如果要初始化具有行的特定值的2D矢量,您可以简单地使用不同的构造函数:

int main()
{
    vector<float> y = {1, 2, 3};  // row template object
    vector<vector<float>> x(10,y); // 2d vector with ten rows, each being a copy of row template y

    for (auto r : x) {
        for (auto c : r) {
            cout << c << " ";
        }
        cout << endl;
    }

    return 0;
}