GVKun编程网logo

【C++ 注意事项】7 Library vector Type(c++注意点)

19

对于想了解【C++注意事项】7LibraryvectorType的读者,本文将是一篇不可错过的文章,我们将详细介绍c++注意点,并且为您提供关于/usr/include/glib-2.0/glib/g

对于想了解【C++ 注意事项】7 Library vector Type的读者,本文将是一篇不可错过的文章,我们将详细介绍c++注意点,并且为您提供关于/usr/include/glib-2.0/glib/gtypes.h:34:24: fatal error: glibconfig.h: No such file or directory、Android Library 开发注意事项、android – nativeLibraryDirectories = [/ vendor / lib64,/ system / lib64]]]找不到“libvideoeditor_jni.s、android – nativeLibraryDirectories = [/ vendor / lib64,/ system / lib64]]]找不到“libvideoeditor_jni.so”的有价值信息。

本文目录一览:

【C++ 注意事项】7 Library vector Type(c++注意点)

【C++ 注意事项】7 Library vector Type(c++注意点)

List Initializer or Element Count?

In a few cases, what initialization means depends upon whether we use curly braces or parentheses to pass the initializer(s). For example, when we initialize a vector< int > from a single int value, that value might represent the vector’s size or it might be an element value. Similarly, if we supply exactly two int values, those values could be a size and an initial value, or they could be values for a two-element vector. We specify which meaning we intend by whether we use curly braces or parentheses:

vector<int> v1(10);  // v1 has ten elements with value 0
vector<int> v2{10};  // v2 has one elements with value 10
vector<int> v3(10,1);  // v3 has tem elements with vlaue 1
vector<int> v4{10,1};  // v4 has two elements with values 10 and 1

When we use parentheses, we are saying that the values we supply are to be used to construct the object. Thus, v1 and v 3 use their initializers to determine the vector’s size, and its size and element values, respectively.

When we use curly braces, {…}, we’re saying that, if possible, we want to list initialize the object. That is, if there is a way to use the values inside the curly braces as a list of element initializers, the class will do so. Only if it is not possible to list initialize the object will the other ways to initialize the object be considered. The values we supply when we initialize v2 and v4 can be used as element values. These objects are list initialized; the resulting vectors have one and two elements, respectively.

On the other hand, if we use braces and there is no way to use the initializers to list initialize the object, then those values will be used to construct the object. For example, to list initialize a vector of strings, we must supply values that can be used as strings. In this case, there is no confusion about whether to list initialize the elements or constructs a vector of the given size:

vector<string> v5{"hi"};  // list initialization: v5 has one element
vector<string> v6("hi");  // error: can''t construct a vector from a string literal
vector<string> v7{10};  // v7 has ten default-initialized elements
vector<string> v8{10,"hi"};  // v8 has tem elements with value "hi"

Although we used braces on all but one of these definitions, only v5 is list initialized. In order to list initialize the vector, the values inside braces must match the element type. We can’t use an int to initialize a string, so the initializers for v7 and v8 can’t be element initializers. If list initialization isn’t possible, the compiler looks for other ways to initialize the object from the given values.

Adding Elements to a vector

As one example, if we need a vector with values from 0 to 9, we can easily use list initialization. What if we wanted elements from 0 to 99 or 0 to 999 ? List initialization would be too unwieldy. In such cases, it is better to create an empty vector and use a vector member named push_back to add elements at run time.

The push_back operation takes a value and “pushes” that value as a new last element onto the “back” of the vector.

vector<int> v2;  // empty vector
for(int i=0; i!= 100; ++i)
    v2.push_back(i);  // append sequential integers to v2
// at end of loop v2 has 100 elements, values 0 ... 99

We use the same approach when we want to create a vector where we don’t know until run time how many elements the vector should have. For example, we might read the input, storing the values we read in the vector:

// read words from the standard input and store them as elements in a vector
string word;
vector<string> text;  // empty vector
while(cin>> word)
    text.push_back(word);  // append word to text

Again, we start with an initially empty vector. This time, we read and store an unknown number of values in text.

Starting with an empty vector and adding elements at run time is distinctly different from how we use built-in arrays in C and in most other languages. In particular, if you are accustomed to using C or Java, you might expect that it would be best to define the vector at its expected size. In fact, the contrary is usually the case.

Other vector Operations

In addition to push_back, vector provide only a few other operations, most of which are similar to the corresponding operations on strings.

Operations Notes
v.empty() Returns true if v is empty; otherwise returns false.
v.size() Returns the number of elements in v.
v.push_back(t) Adds an element with value t to end of v.
v[n] Returns a reference to the element at position n in v.
v1=v2 Replaces the elements in v1 with a copy of the elements in v2.
v1={a,b,c…} Replaces the elements in v1 with a copy of the elements in the comma-separated list.
v1==v2,v1!=v2 v1 and v2 are equal if they have the same number of elements and each element in v1 is equal to the corresponding element in v2.
<, <=, >, >= Have their normal meaning using dictionary ordering.

We access the elements of a vector the same way that we access the characters in a string: through their position in the vector. For example, we can use a range for to process all the elements in a vector:

vector<int> v{1,2,3,4,5,6,7,8,9};
for(auto &i: v)  // for each element is v (note: i is a reference)
    i*= i;  // square the element value
for(auto i: v)  // for each element in v
    cout<< i << " ";  // print the element
cout<<endl;

The output should be

1 4 9 16 25 36 49 64 81

To use size_type, we must name the type in which it is defined. A vector type always includes its element type:

vector<int>:: size_type  // ok
vector::size_type  // error

Subscripting Does Not Add Elements

Programmers new to C++ sometimes think that subscripting a vector adds elements; it does not. The following code intends to add tem elements to ivec:

vector<int> ivec;  // empty vector
for(decltype(ivec.size()) ix= 0; ix!= 10; ++ix)
    ivec[ix]= ix;  // disaster: ivec has no elements

However, it is in error: ivec is an empty vector; there are no elements to subscript! As we’ve seen, the right way to write this loop is to use push_back:

for(decltype(ivec.size()) ix= 0; ix!= 10; ++ix)
    ivec.push_back(ix);  // ok: adds a new element with value x

The subscript operator on vector (and string) fetches an existing element; it does not add an element.

Subscript Only Elements that are Known to Exist!

It is crucially important to understand that we may use the subscript operator (the [] operator) to fetch only elements that actually exist. For example,

vector<int> ivec;  // empty vector
cout<< ivec[10];  // error: ivec has no elements!
vector<int> ivec2(10);  // vector with ten elements
cout<< ivec2[10];  // error: ivec2 has elements 0...9

It is an error to subscript an element that doesn’t exist, but it is an error that the compiler is unlikely to detect. Instead, the value we get at run time is undefined.

Attempting to subscript elements that do not exist is, unfortunately, an extremely common and pernicious programming error. So-called buffer overflow errors are the result of subscripting elements that don’t exist. Such bugs are the most common cause of security problems in PC and other applications.

版权声明:本文为 NoMasp 柯于旺 原创文章,未经许可严禁转载!欢迎访问我的博客:http://blog.csdn.net/nomasp

/usr/include/glib-2.0/glib/gtypes.h:34:24: fatal error: glibconfig.h: No such file or directory

/usr/include/glib-2.0/glib/gtypes.h:34:24: fatal error: glibconfig.h: No such file or directory

cc -DDEBUG -mtune=core2 -O2 \
-onvideo nvideo.c \
-I/usr/include/atk-1.0 \
-I/usr/include/cairo \
-I/usr/include/gdk-pixbuf-2.0 \
-I/usr/include/glib-2.0 \
-I/usr/include/gtk-3.0 \
-I/usr/include/pango-1.0 \
-I/usr/include/libxml2 \
-I/usr/lib/x86_64-linux-gnu/glib-2.0/include \
-lpthread \
-lgtk-3 -lgobject-2.0 -lgthread-2.0 \
-lxml2
In file included from /usr/include/glib-2.0/glib/galloca.h:34:0,
from /usr/include/glib-2.0/glib.h:32,
from /usr/include/gtk-3.0/gdk/gdkconfig.h:13,
from /usr/include/gtk-3.0/gdk/gdk.h:32,
from /usr/include/gtk-3.0/gtk/gtk.h:32,
from nvideo.c:11:
/usr/include/glib-2.0/glib/gtypes.h:34:24: fatal error: glibconfig.h: No such file or directory
compilation terminated.
make: *** [edit] Error 1

因为之前已经安装了 gtk 开发包,所以理论上不应该出现找不到.h 文件的问题。最后发现问题出在 - I/usr/lib/x86_64-linux-gnu/glib-2.0/include 参数上。因为之前使用的 64 位,所以这个位置没有问题,但是现在我用的是 32 位系统,所以导致这个路径指错了,增加 - I/usr/lib/i386-linux-gnu/glib-2.0/include 即可。

实际使用应该使用 pkg-config 来配置这个路径的,但是我想手工去完成这个事情,所以导致了这个问题。还弄得满世界去找是不是少装了哪个开发包~~:)

Android Library 开发注意事项

Android Library 开发注意事项

Android Library 开发注意事项

App Module 添加依赖 Android Library 时可以设置 library 的优先级,

在编译时,app 按照 library 从低到高的优先级依次与每个 library 合并。

开发 Android Library 和相关 APP 时,请注意下面事项:

  • 资源合并冲突:编译工具会合并 library 和 app 的资源。如果某个 resource ID 在两个 Module 中都定义了,那么会使用 app 的资源。 如果冲突发生在多个 AAR 之间,那么会优先使用 dependencies 列表中排在前面的 library 的资源。 为了防止 Module 直接资源冲突,请给每个 Module 的资源使用唯一的前缀或命名空间,就像用包名唯一确认一个 APP 一样。

  • Android Library 可以包含 JAR Library 可以在 Android Library 中使用 JAR Library,并且依赖这个 Android Library 的 App Module 也需要配置好对这个 JAR Library 的引用。

  • Android Library 可以依赖 external JAR library Android Library 可以依赖 external JAR library,如一个地图的 external library,那么依赖这个 Android Library 的 App Module 编译时必须要依赖包含这个 external library 的 target,如 Google APIs Add-On。Android Library Module 和 App Module 都必须要在 Manifest 文件中用 <uses-library> 申明使用这个 external library。

  • App Module 的 minSdkVersion 必须等于或大于 Android Library 的 minSdkVersion。 Android Library 是作为 App Module 的一部分被编译的,所以它使用的 API 必须要与 App Module 支持的平台版本相匹配。

  • 每个 Android Library 独自创建其 R class 当编译 App Module 时,Android Library 被编译成 AAR 文件然后被添加到 App Module。所以每个 Android Library 有其独有的 R class,并根据其包名命名。App Module 和 Android Library 的 R class 被生成长它们各自的 package 下。

  • Android Library 可以有独自的 ProGuard 配置文件 每个 Android Library 可以有自己的 ProGuard 配置文件,编译工具会把这个文件嵌入到生成的 AAR 文件中。当 Android Library 添加到 App Module 时,library 的 ProGuard 文件会附加到 App Module 的 ProGuard 文件。当 App Module 运行 ProGuard 文件时,它会运行 App 和 library 的 ProGuard 文件,所以你不需要单独运行 library 的 ProGuard 文件。

  • 测试 Android Library 与测试 App 一样 主要的不同是 Android Library 和它依赖的 dependencies 自动被包含成 Test APK 的依赖项。即 Test APK 不仅包含其自身的代码,还包含 Android Library 的 AAR 和相关依赖。因为没有单独的”app under test”,所以 androidTest 任务只安装 / 卸载 Test APK。

当合并多个 Manifest 文件时,Gradle 按照默认的优先级顺序把 library 的 manifest 合并到 APP 的 manifest。

AAR 文件的结构

AAR 文件的后缀名是.aar,且在 Maven 中的类型也是 aar。 AAR 文件本身是一个 zip 文件,包括下面内容:

  • /AndroidManifest.xml
  • /classes.jar
  • /res/
  • /R.txt
  • /public.txt

通常 AAR 文件可能包含下面的一个或多个可选文件

  • /assets/
  • /libs/name.jar
  • /jni/abiname/name.so ( abiname 是 Android 支持的一种 ABI)
  • /proguard.txt
  • /lint.jar

关于我

私人博客

微信公众号:infree6 或者直接扫码

android – nativeLibraryDirectories = [/ vendor / lib64,/ system / lib64]]]找不到“libvideoeditor_jni.s

android – nativeLibraryDirectories = [/ vendor / lib64,/ system / lib64]]]找不到“libvideoeditor_jni.s

java.lang.UnsatisfiedLinkError:dalvik.system.PathClassLoader [DexPathList [[zip file“/data/app/com.miimobileapp.miivdo-1/base.apk\”],nativeLibraryDirectories=[/vendor/lib64,/ system / lib64] ]]找不到“libvideoeditor_jni.so”

解决方法:

请尝试以下gradle设置:

gradle.properties:
android.useDeprecatedndk=true

build.gradle
android {
    ...
    defaultConfig {
        ...
        ndk {
            abiFilters "armeabi-v7a", "x86", "armeabi"
        }
    }
}

android – nativeLibraryDirectories = [/ vendor / lib64,/ system / lib64]]]找不到“libvideoeditor_jni.so”

android – nativeLibraryDirectories = [/ vendor / lib64,/ system / lib64]]]找不到“libvideoeditor_jni.so”

java.lang.UnsatisfiedLinkError:dalvik.system.PathClassLoader [DexPathList [[zip file“/data/app/com.miimobileapp.miivdo-1/base.apk\”],nativeLibraryDirectories=[/vendor/lib64,/ system / lib64] ]]找不到“libvideoeditor_jni.so”

解决方法

请尝试以下gradle设置:

gradle.properties:
android.useDeprecatedndk=true

build.gradle
android {
    ...
    defaultConfig {
        ...
        ndk {
            abiFilters "armeabi-v7a","x86","armeabi"
        }
    }
}

关于【C++ 注意事项】7 Library vector Typec++注意点的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于/usr/include/glib-2.0/glib/gtypes.h:34:24: fatal error: glibconfig.h: No such file or directory、Android Library 开发注意事项、android – nativeLibraryDirectories = [/ vendor / lib64,/ system / lib64]]]找不到“libvideoeditor_jni.s、android – nativeLibraryDirectories = [/ vendor / lib64,/ system / lib64]]]找不到“libvideoeditor_jni.so”等相关内容,可以在本站寻找。

本文标签: