Should state vectors be considered constant? By the principle of superposition, a state vector can be defined as
$$\begin{align}
\psi(x) &= c_1 \psi_1(x) + c_2 \psi_2(x) + \cdots + c_n \psi_n(x) \\
\lvert\psi\rangle &= \begin{pmatrix}c_1 \\ c_2 \\ \vdots \\ c_n\end{pmatrix} = c_1\lvert\varepsilon_1\rangle + c_2\lvert\varepsilon_2\rangle + \cdots + c_n\lvert\varepsilon_n\rangle
\end{align}$$
where $c_n = \langle \varepsilon_n\lvert \psi\rangle$.
Also, the state vector can represent a wave function in a continuous case as in
$$\begin{align}
\lvert\psi\rangle
&= \begin{pmatrix}\psi(x\to -\infty) \\ \vdots \\ \psi(x=0) \\ \vdots \\ \psi(x = 0.000\ldots 01) \\ \psi(x = 0.000\ldots 02) \\ \vdots \\ \psi(x\to\infty)\end{pmatrix} \tag{1} \\
&= \psi(x\to -\infty)\lvert x\to -\infty\rangle + \cdots + \psi(x = 0)\lvert x=0\rangle + \cdots
\end{align}$$
My first question is, in the sequence of expressions
$$\begin{align}
\langle\psi\lvert\psi\rangle
&= \int_{-\infty}^{+\infty}\psi^{*}(x)\psi(x)\ \mathrm{d}x \\
&= \int_{-\infty}^{+\infty}\langle x\lvert\psi\rangle^* \langle x\lvert\psi\rangle\ \mathrm{d}x \\
&= \int_{-\infty}^{+\infty}\langle\psi\lvert x\rangle \langle x\lvert\psi\rangle\ \mathrm{d}x \\
&= \color{red}{\langle\psi\lvert}\int_{-\infty}^{+\infty}\lvert x\rangle \langle x\rvert\ \mathrm{d}x\ \color{red}{\lvert\psi\rangle} \\
&= \langle\psi\rvert \hat{\mathbb{1}}\lvert\psi\rangle
\end{align}$$
with $\hat{\mathbb{1}} = \int_{-\infty}^{+\infty}\mathrm{d}x\ \lvert x\rangle\langle x\rvert$, why can the state vector can be pulled out from the integral?
I have an idea but I think it should be wrong:
As shown in equation (1), the state vector is a set of values such that each element is the value of the wave function, e.g. $\psi(0)$, $\psi(0.002)$, etc. Thus, it can be considered as a constant since each element will never change. Therefore, it can be pulled out from the integral. 
However, this idea fails to explain the observable case. Consider an operator which contains a momentum component, a differential operator, $\hat{Q}(x, \frac{\hbar}{i}\frac{\mathrm{d}}{\mathrm{d}x})$. The expectation value of this operator is
$$\begin{align}
\langle\hat{Q}\rangle
&= \int_{-\infty}^{+\infty}\psi^*(x)\hat{Q}\psi(x)\ \mathrm{d}x \\
&= \int_{-\infty}^{+\infty}\langle x\lvert\psi\rangle^*\hat{Q}\langle x\lvert\psi\rangle\ \mathrm{d}x \\
&= \int_{-\infty}^{+\infty}\langle\psi\lvert x\rangle\hat{Q}\langle x\lvert\psi\rangle\ \mathrm{d}x \\
&= \color{red}{\langle\psi\rvert}\int_{-\infty}^{+\infty}\lvert x\rangle\hat{Q}\langle x\rvert\ \mathrm{d}x\ \color{red}{\lvert\psi\rangle} \\
&= \langle\psi\rvert\int_{-\infty}^{+\infty}\mathrm{d}x\ \lvert x\rangle\langle x\rvert\color{blue}{\hat{Q}}\lvert\psi\rangle \tag{2} \\
&= \langle\psi\rvert\hat{\mathbb{1}}\hat{Q}\lvert\psi\rangle \\
&= \langle\psi\rvert\hat{Q}\lvert\psi\rangle
\end{align}$$
If the state vector is constant, it should give zero when a momentum operator acts on it, and the Dirac representation will fail.


*

*So, if it is not a constant, how can it be pulled out from the integral?

*My second question is that, in equation (2), why can the operator $\hat{Q}$ skip the $\langle x\rvert$ vector and act on the state vector?
 A: First of all I would like to point out that what you have written in equation (1) is not the most general way of writing the state vector. Instead you have chose to write it in terms of a certain basis for the Hilbert space, namely the position basis in this case. But a-priori the state vector is an object in the Hilbert space and basis independent. What is basis dependent are the components (i.e. the expansion coefficients of the vector in the basis) that you wrote down in (1).
This explains why $\lvert\psi\rangle$ comes out of the integral. The state vector itself is independent of what basis you have chosen. You could write this as $$\rvert \psi\rangle = \int_{-\infty}^{+\infty} \lvert x\rangle\langle x\rvert \psi\rangle \mathrm{d}x=\int_{-\infty}^{+\infty} \lvert x\rangle \psi(x) \mathrm{d}x$$. So here $\psi(x)$ are the components of $\rvert \psi\rangle$ in the x-basis. Note that since the x is integrated over it is not "visible" from outside the integral, i.e. $\rvert \psi\rangle$ does not know about it. We could insert that into your expression for the inner product to give:
$$\langle\psi\rvert\psi\rangle=\langle\psi\rvert\int_{-\infty}^{+\infty} \lvert x\rangle\langle x\rvert \mathrm{d}x\ \rvert \psi\rangle= \int_{-\infty}^{+\infty} \langle x'\rvert \psi(x') \mathrm{d}x'\int_{-\infty}^{+\infty} \lvert x\rangle\langle x\rvert \mathrm{d}x\ \int_{-\infty}^{+\infty} \lvert x''\rangle \psi(x'') \mathrm{d}x''$$
Note that x, x' and x'' are dummy variables and the information that it is the x-basis lies in writing $\lvert x\rangle$ in the intergrals.
Now for your equation (2) the same thing applies. The operator a-priori lives in the Hilbert space, it acts on the state vectors. But you can choose to express it in a certain basis:
$$ \hat{Q} = \int_{-\infty}^{+\infty} \hat{Q}\lvert x\rangle\langle x\rvert \mathrm{d}x = \int_{-\infty}^{+\infty} Q(x) \lvert y(x)\rangle\langle x\rvert \mathrm{d}x = \int_{-\infty}^{+\infty} Q(x) \lvert x\rangle\langle x\rvert \mathrm{d}x$$
where the last step only applies if the operator is diagonal in the x-basis (for states we don't have this complication, I only added this step to show the similarity to the state expansion).
